locked
PowerShell Random Generator RRS feed

  • Question

  • I have been trying to create a random room / user list by taking the room list and assigning a user to that room at random.  I have been able to do this with the following code:

    $rooms = Get-Content "rooms.txt"

    $users = Get-Content "users.txt"

    $results = Foreach ($room in $rooms) {[pscustomobject]@{ Rooms = $room; User = Get-Random -InputObject $users}}

    $results | Export-Csv "results.csv" -NoTypeInformation

    What I need help with is how can I ensure every user is assigned about the same number of rooms.  example: 10 rooms + 3 users = two users have 3 rooms and one user has 4.  What I'm getting now is 1 user has 1 room, 1 user has 4 rooms, and 1 user has 5 rooms.  I need them somewhat even to be fair.  

    • Moved by Bill_Stewart Wednesday, September 4, 2019 8:55 PM This is not "design solution for me" forum
    Tuesday, April 9, 2019 8:06 PM

All replies

  • You will have to write you own room assignment code.

    You can try this.

    $idx = Get-Random  -Maximum $rooms.Count -Minimum 1
    $rooms[$idx]


    \_(ツ)_/

    Tuesday, April 9, 2019 8:25 PM
  • You have made the problem difficult. It is easy to assign a random user to a room, but then it becomes difficult to avoid the distortions you encountered. It is easy to assign about the same number of users to each room (the number varying by no more than 1) but then the results are not random. The only way I can think to solve this is to keep track of the number of rooms assigned to each user (perhaps in an array). Then as rooms are assigned, select a user at random and check if the user about to be assigned already has the maximum you want. If the user already has the maximum, somehow try another random user, and repeat until you find a user with less than the maximum.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, April 9, 2019 9:58 PM
  • THe issue is a mathematical one.  Distribute N items to M containers.

    The OP

    s question is backwards which will force the result ot be harder to obtain.

    The question should be - "how do we assign N users to M rooms.

    Enumerate the rooms and assign users from a queue until all users have been assigned.

    I don't see where randomness can have anything to do with this or how it would ever be of any help.  Maybe there is moe information that would make this a real world issue but, as stated, it is just random thinking.


    \_(ツ)_/

    Tuesday, April 9, 2019 10:37 PM
  • This problem made me think. I decided we can take advantage of the fact that hash tables save entries in an apparent random order. I could not code to modify the hash table of rooms. So I enumerated the first hash table of rooms (in random order) and assigned the users to a new hash table.

    # Retrieve users and rooms.
    $Users = Get-Content Users.txt
    $Rooms = Get-Content Rooms.txt
    
    # Determine the number of users and rooms.
    $NumUsers = $Users.Count
    "Total number of users: $NumUsers"
    $NumRooms = $Rooms.Count
    "Total number of rooms: $NumRooms"
    
    # Hash table. The key is a room, the value is a $True.
    $RdmRooms = @{}
    
    # Add the rooms to a hash table.
    # The order of rooms in this hash table will appear to be random.
    ForEach ($Room In $Rooms)
    {
        $RdmRooms.Add($Room, $True)
    }
    
    # New hash table of rooms and the users assigned.
    $Result = @{}
    
    # Index number of a user.
    $i = 0
    
    # Increment through the hash table of rooms.
    # Create a new hash table of rooms and users assigned to the rooms.
    ForEach ($Key In $RdmRooms.Keys)
    {
        # Assign the user to this room.
        $Result[$Key] = $Users[$i]
        # Increment the user index.
        $i = $i + 1
        If ($i -gt $NumUsers - 1) {$i = 0}
    }
    
    $Result
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, April 9, 2019 11:36 PM