# [Tutor] question

dn PythonList at DancesWithMice.info
Wed Apr 12 05:49:42 EDT 2023

```On 12/04/2023 20.41, Prayash Bhuyan wrote:
> import random
>
> # list of names
> names_list = ["ayush","bhargav","akankhya","prayash"]
>
> # dictionary to keep track of numbers assigned to names
> #numbers_dict = {}
>
> # prompt the user for a name and randomly assign a number to the name
> for name in names_list:
>      while True:
>          user_input = input("Enter name: ")
>          if user_input.casefold() in names_list:
>              group = random.randint(1, 4)
>              #numbers_dict[user_input] = number
>              break
>          else:
>              print("Name not in list. Please try again.")
>
> this is my code i am writing a code where the computer asks the user for
> a name and if the name is in the list then it randomly assigns a number
> which in this case is the group number ..Now suppose I have 20 members
> in the lsit and I want to form four groups with five members in each
> group ..now a particular random number between 1 and 4 should only
> appear 5 times and not more than that because if that occurs members
> will not be equally divided in the group ..thats what i want...and it is
> not an assignment it's something that i am doing to satisfy my curiosity

It would seem that what is required is not entirely random. There must
be a uniform distribution of people to groups.

It is more a question of algorithm, than of Python - but around here we
like challenges!

Choice 1: process-oriented

- outer loop: for each name
- inner loop: while 'name is unassigned'
- randomly choose a groupNR in range( NRgroups )
- if group is not full
- assign name to group
- break

The first name will only require one cycle of the inner loop because
whichever group is randomly chosen will be empty.

As names are assigned, the chance that the inner-loop has to be repeated
will increase.

No matter how many loops are required, eventually the last loop will
fill the last spot in the last group to be filled.

Choice 2: data-oriented

- preparation step
- create a list of group-numbers in range( NRgroups )
& using itertools.cycle() or .repeat(), create a uniformly-distributed
range of group-numbers, the same length as len( names_list ).

- outer loop: for each name
- use len( group_numbers ) as the number of unassigned names
(or places in groups).
- create a random integer in range( unassignedNR )
- use that as an index to pop() a random group_number from the
list of group-numbers

NB pop() removes that indexed-element from the list, and thus shortens
it - hence looking at the len() each time around the loop

As each name is assigned, the remaining/unassigned group_number-s will
be reduced.

Because the group_numbers were pre-defined and pre-distributed, there is
no need to count how many members have been previously assigned to a
group - the (math involved in the) preparation step takes care of that!

Do you follow the two lines-of-logic?

NB I'm sure our colleagues will come-up with other ideas!

> On Wed, Apr 12, 2023 at 1:21 PM dn via Tutor <tutor at python.org
> <mailto:tutor at python.org>> wrote:
>
>     On 12/04/2023 18.28, Prayash Bhuyan wrote:
>      > I know how to generate random numbers using random function but I
>     want to
>      > know how do i generate random numbers say between (1,5) where
>     each random
>
>     Do you know how to write a loop in Python?
>     Do you know about dict[ionary]s?
>     Are we talking about integers or real numbers?
>     (or suites in a deck of cards)
>     Is this a course-assignment?
>
>     then we will be able to discuss what's missing or what might be added.
>
>     --
>     Regards,
>     =dn
>     _______________________________________________
>     Tutor maillist  - Tutor at python.org <mailto:Tutor at python.org>
>     To unsubscribe or change subscription options:
>     https://mail.python.org/mailman/listinfo/tutor
>     <https://mail.python.org/mailman/listinfo/tutor>
>

--
Regards,
=dn

```