randomly generating groups of people via values in a list

random group generator
random group generator without repeats
random group name generator
how to randomly assign participants to groups
random pairing generator from two lists
random team generator with conditions
how to randomly assign participants to equal groups in excel
name match generator

I am writing a program to randomly generate groups for the office scavenger hunt I am organizing so I wrote this simple code quickly to simulate picking names out of a hat but only more fairer, but not exactly sure why it isn't working, it doesn't return all the names, for example I have entered 6 names into the list but it only returns 4 of them like this:

Group 1 consists of;
Chris
Ryan
Paul
Group 2 consists of;
Alex

I don't have much experience with removing elements from a list so it could well just be me doing it wrong. Any insight would be helpful.

import random
participants=["Alex","Elsie","Elise","Kimani","Ryan","Chris","Paul"]
group=1
membersInGroup=3

for participant in participants:
    if membersInGroup==3:
        print("Group {} consists of;".format(group))
        membersInGroup=0
        group+=1
    person=random.choice(participants)
    print(person)
    membersInGroup+=1
    participants.remove(str(person))

This issue is happening because you are mutating a list while iterating over it. There are a few options for solving this. One option that requires little modification to your existing code and thought process is to mutate a copy of the list, e.g. participants[:] makes a copy.

Using your code:

import random
participants=["Alex","Elsie","Elise","Kimani","Ryan","Chris","Paul"]
group=1
membersInGroup=3

for participant in participants[:]:               # only modification
    if membersInGroup==3:
        print("Group {} consists of;".format(group))
        membersInGroup=0
        group+=1
    person=random.choice(participants)
    print(person)
    membersInGroup+=1
    participants.remove(str(person))

Sample output:

Group 1 consists of;
Alex
Paul
Chris
Group 2 consists of;
Kimani
Elsie
Elise
Group 3 consists of;
Ryan

Excel formula: Randomly assign people to groups, To randomly assign people to groups or teams of a specific size, you can use a helper column with a value generated by the RAND function, actual values using paste special to prevent changes after random values are assigned. The RANK function is used to rank the value in C5 against all random values in the list. In column D, a group number is assigned with the following formula: = ROUNDUP(RANK( C5, randoms) / size,0) The RANK function is used to rank the value in C5 against all random values in the list. The result will be a number between 1 and the total number of people (12 in this example). This result is then divided by "size", which represents the desired group size (3 in the example), which then goes into the ROUNDUP function as number, with num_digits of zero.

Although pylangs' answer will suffice, this is another approach to solve the problem:

import random
members = 3
participants=["Alex","Elsie","Elise","Kimani","Ryan","Chris","Paul"]
random.shuffle(participants)
for i in range(len(participants) // members + 1):
    print('Group {} consists of:'.format(i + 1))
    group = participants[i*members:i*members + members]
    for participant in group:
        print(participant)

Excel formula: Randomly assign data to groups, If you need a formula approach to randomly assign people to groups of a fixed you can use a helper column with a value generated by the RAND function, To get a random value from a table or list in Excel, you can use the INDEX The Excel CHOOSE function returns a value from a list using a given position or index . To randomly people (or anything) to groups you can use the RANDBETWEEN function with the CHOOSE function. In the example shown, the formula in F3 is: = CHOOSE(RANDBETWEEN(1,3),"A","B","C") When copied down the column, this formula will generate a random group (A, B, or C) for each person in the list.

It depends on the type of list. The code has the problem in line

for participant in participants:

Instead use

for participant in participants[:]:

Excel formula: Randomly assign people to groups, Want to learn more about randomly assigning people groups? We also use a helper column where RAND function is used to randomly generate values. Another approach of assigning random value is by using the CEILING function a list of groups (group a, group b etc) and displays that there are 2 people in group 'a'� How to randomly assign people to groups in Excel Here we can use a formula created by the Excel RANK and ROUNDUP functions together. We also use a helper column where RAND function is used to randomly generate values.

Random Selection Generator - Choice Picker - Online Tool, Tools to make random choices, generate random numbers, etc. How to associate items from two groups of the same size? Without list or with too many items, assign a number on objects/people and use the random number generator . Participation in the game is via (indicate how to participate: fill out a form, send a� Generate random value from a given list by formula Actually, there are some easy formulas that can help you to quickly generate random value from a given list. Select a blank cell that will place the random value, type this formula =INDEX($A$2:$A$16, RANDBETWEEN(2, 16)), then press Enterkey.

Quickly generate random groups for list of data in Excel, If you want to randomly assign data to a specified number of groups, each group is allowed with different numbers of data, you can use the CHOOSE and� How to create randomized groups. Enter each item on a new line, choose the amount of groups unders settings, and click the button to generate your randomized list. Don't like the first team? Just click again until you do. Fairly pick teams without bias. No need to draw names out of a hat.

Random Group Generator Template [Free Download], Download Free Random Group Generator Template to quickly divide a list of click on the 'Create Teams' button to randomly generate the groups of names. returns a random number between 1 and the total number of names in the list ( using the values in column B are unique, this formula gives a unique list of integers� In a nutshell, you use the INDEX function to extract a value from the list based on a random row number returned by RANDBETWEEN. More specifically, the RANDBETWEEN function generates a random integer between the two values you specify. For the lower value, you supply the number 1.

Comments
  • Possible duplicate of Difference between del, remove and pop on lists
  • thanks that actually makes so much sense, its little things like that, that change things massively
  • An interesting approach @mohammad. +1