473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to split a list of random numbers equally

I have 250 students in my boarding school to be assigned randomly to 36 dining tables. I have written a Rnd function that does that but, for some tables, it assigns more students than others. How do I rewrite my function to atleast assign equal number of students to each table? Peter Arre, Papua New Guinea
Oct 5 '11 #1

✓ answered by ADezii

I created some Quick Code that Randomizes Student IDs (between 0 and 249), places them into an Array, checks for Duplicates, then sequentially assigns these Student IDs to each of the 36 Tables. The Code can definitely be improved upon, especially since it assigns 7 Random Students to each of the first 35 Tables (245 Students), then leaves the remaining 5 for the last Table (total of 250). I'll post the Code below, along with the generated Output. This should, at least, point you in the right direction.
Expand|Select|Wrap|Line Numbers
  1. 'Assign 250 Students Randomly and relatively evenly to 36 Tables
  2. '************************* User Defined Section *************************
  3. Const conNUMBER_OF_STUDENTS As Byte = 250
  4. Const conNUMBER_OF_TABLES As Byte = 36
  5. Const conSTUDENTS_PER_TABLE As Byte = 7
  6. '************************************************************************
  7.  
  8. Dim intCtr As Integer
  9. Dim intCtr_2 As Integer
  10. Dim aintStudents(0 To conNUMBER_OF_STUDENTS - 1) As Integer
  11.  
  12. 'The 1st 35 Tables will have 7 Students per Table, Tables 36 will have 5
  13. 'Randomize the 250 Students, populate Array wiith the Results
  14. For intCtr = 0 To conNUMBER_OF_STUDENTS - 1
  15.   aintStudents(intCtr) = Int(Rnd * conNUMBER_OF_STUDENTS)
  16. Next
  17.  
  18. 'Lets eliminate the Duplicates
  19. DoItAllOverAgain:
  20. For intCtr = 0 To UBound(aintStudents)
  21.   For intCtr_2 = 0 To UBound(aintStudents)
  22.     If intCtr <> intCtr_2 Then
  23.       If aintStudents(intCtr) = aintStudents(intCtr_2) Then
  24.         aintStudents(intCtr) = Int(Rnd * conNUMBER_OF_STUDENTS)
  25.           GoTo DoItAllOverAgain
  26.       Else
  27.       End If
  28.     End If
  29.   Next intCtr_2
  30. Next intCtr
  31.  
  32. Debug.Print "Number of Students: " & conNUMBER_OF_STUDENTS
  33. Debug.Print "Number of Tables: " & conNUMBER_OF_TABLES
  34. Debug.Print "Students per Table: " & conSTUDENTS_PER_TABLE
  35.  
  36. 'aintStudents() now contains 250 Random and Unique Values between 0 and 249
  37. 'Let's now assign to the 36 Tables
  38.   For intCtr_2 = 0 To conNUMBER_OF_STUDENTS - 1
  39.    If intCtr_2 Mod conSTUDENTS_PER_TABLE = 0 Then Debug.Print "*********************************"
  40.      Debug.Print Format(intCtr_2 + 1, "000") & " - Student #" & Format$(aintStudents(intCtr_2) + 1, "000") & _
  41.                  " ==> " & "Table #" & Format$(intCtr_2 \ conSTUDENTS_PER_TABLE + 1, "00")
  42.   Next
  43.   Debug.Print "*********************************"
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Number of Students: 250
  2. Number of Tables: 36
  3. Students per Table: 7
  4. *********************************
  5. 001 - Student #007 ==> Table #01
  6. 002 - Student #036 ==> Table #01
  7. 003 - Student #221 ==> Table #01
  8. 004 - Student #160 ==> Table #01
  9. 005 - Student #032 ==> Table #01
  10. 006 - Student #122 ==> Table #01
  11. 007 - Student #097 ==> Table #01
  12. *********************************
  13. 008 - Student #142 ==> Table #02
  14. 009 - Student #050 ==> Table #02
  15. 010 - Student #168 ==> Table #02
  16. 011 - Student #244 ==> Table #02
  17. 012 - Student #145 ==> Table #02
  18. 013 - Student #062 ==> Table #02
  19. 014 - Student #031 ==> Table #02
  20. *********************************
  21. 015 - Student #049 ==> Table #03
  22. 016 - Student #178 ==> Table #03
  23. 017 - Student #088 ==> Table #03
  24. 018 - Student #166 ==> Table #03
  25. 019 - Student #131 ==> Table #03
  26. 020 - Student #109 ==> Table #03
  27. 021 - Student #018 ==> Table #03
  28. *********************************
  29. 022 - Student #192 ==> Table #04
  30. 023 - Student #161 ==> Table #04
  31. 024 - Student #058 ==> Table #04
  32. 025 - Student #132 ==> Table #04
  33. 026 - Student #194 ==> Table #04
  34. 027 - Student #234 ==> Table #04
  35. 028 - Student #185 ==> Table #04
  36. *********************************
  37. 029 - Student #203 ==> Table #05
  38. 030 - Student #180 ==> Table #05
  39. 031 - Student #138 ==> Table #05
  40. 032 - Student #022 ==> Table #05
  41. 033 - Student #237 ==> Table #05
  42. 034 - Student #247 ==> Table #05
  43. 035 - Student #099 ==> Table #05
  44. *********************************
  45. 036 - Student #239 ==> Table #06
  46. 037 - Student #213 ==> Table #06
  47. 038 - Student #143 ==> Table #06
  48. 039 - Student #125 ==> Table #06
  49. 040 - Student #054 ==> Table #06
  50.  
  51. 'Output initentially omitted
  52.  
  53. *********************************
  54. 246 - Student #041 ==> Table #36
  55. 247 - Student #177 ==> Table #36
  56. 248 - Student #038 ==> Table #36
  57. 249 - Student #068 ==> Table #36
  58. 250 - Student #121 ==> Table #36
  59. *********************************

6 3159
Rabbit
12,516 Expert Mod 8TB
Basically, the steps would be:

1) Assign each person a random number
2) Sort by the random number
3) Sequentially number the sorted list
4) Then you can mod the sequential number to assign them to a table or you can just assign them in ranges
Oct 5 '11 #2
NeoPa
32,556 Expert Mod 16PB
  1. Assign all your students, in any order, to an array of n elements where n is the number of students you have (250 in this case).
  2. Cycle through your tables one at a time.
  3. For each table take a new random number and multiply it by the number of students left to assign (Starts at 250 but reduces for each student assigned).
  4. Using this value as an index into the student array take the indicated student and add it to the table.
  5. Replace the recently selected student in the array with the last student in the array.
  6. Reduce the array elements by one (or simply use only the array elements covered by the new value of unassigned students).
  7. Continue until all students have been assigned.
Oct 5 '11 #3
@Rabbit
Thank you VM your tip is pointing me in the right direction P. Arre
Oct 8 '11 #4
ADezii
8,834 Expert 8TB
I created some Quick Code that Randomizes Student IDs (between 0 and 249), places them into an Array, checks for Duplicates, then sequentially assigns these Student IDs to each of the 36 Tables. The Code can definitely be improved upon, especially since it assigns 7 Random Students to each of the first 35 Tables (245 Students), then leaves the remaining 5 for the last Table (total of 250). I'll post the Code below, along with the generated Output. This should, at least, point you in the right direction.
Expand|Select|Wrap|Line Numbers
  1. 'Assign 250 Students Randomly and relatively evenly to 36 Tables
  2. '************************* User Defined Section *************************
  3. Const conNUMBER_OF_STUDENTS As Byte = 250
  4. Const conNUMBER_OF_TABLES As Byte = 36
  5. Const conSTUDENTS_PER_TABLE As Byte = 7
  6. '************************************************************************
  7.  
  8. Dim intCtr As Integer
  9. Dim intCtr_2 As Integer
  10. Dim aintStudents(0 To conNUMBER_OF_STUDENTS - 1) As Integer
  11.  
  12. 'The 1st 35 Tables will have 7 Students per Table, Tables 36 will have 5
  13. 'Randomize the 250 Students, populate Array wiith the Results
  14. For intCtr = 0 To conNUMBER_OF_STUDENTS - 1
  15.   aintStudents(intCtr) = Int(Rnd * conNUMBER_OF_STUDENTS)
  16. Next
  17.  
  18. 'Lets eliminate the Duplicates
  19. DoItAllOverAgain:
  20. For intCtr = 0 To UBound(aintStudents)
  21.   For intCtr_2 = 0 To UBound(aintStudents)
  22.     If intCtr <> intCtr_2 Then
  23.       If aintStudents(intCtr) = aintStudents(intCtr_2) Then
  24.         aintStudents(intCtr) = Int(Rnd * conNUMBER_OF_STUDENTS)
  25.           GoTo DoItAllOverAgain
  26.       Else
  27.       End If
  28.     End If
  29.   Next intCtr_2
  30. Next intCtr
  31.  
  32. Debug.Print "Number of Students: " & conNUMBER_OF_STUDENTS
  33. Debug.Print "Number of Tables: " & conNUMBER_OF_TABLES
  34. Debug.Print "Students per Table: " & conSTUDENTS_PER_TABLE
  35.  
  36. 'aintStudents() now contains 250 Random and Unique Values between 0 and 249
  37. 'Let's now assign to the 36 Tables
  38.   For intCtr_2 = 0 To conNUMBER_OF_STUDENTS - 1
  39.    If intCtr_2 Mod conSTUDENTS_PER_TABLE = 0 Then Debug.Print "*********************************"
  40.      Debug.Print Format(intCtr_2 + 1, "000") & " - Student #" & Format$(aintStudents(intCtr_2) + 1, "000") & _
  41.                  " ==> " & "Table #" & Format$(intCtr_2 \ conSTUDENTS_PER_TABLE + 1, "00")
  42.   Next
  43.   Debug.Print "*********************************"
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Number of Students: 250
  2. Number of Tables: 36
  3. Students per Table: 7
  4. *********************************
  5. 001 - Student #007 ==> Table #01
  6. 002 - Student #036 ==> Table #01
  7. 003 - Student #221 ==> Table #01
  8. 004 - Student #160 ==> Table #01
  9. 005 - Student #032 ==> Table #01
  10. 006 - Student #122 ==> Table #01
  11. 007 - Student #097 ==> Table #01
  12. *********************************
  13. 008 - Student #142 ==> Table #02
  14. 009 - Student #050 ==> Table #02
  15. 010 - Student #168 ==> Table #02
  16. 011 - Student #244 ==> Table #02
  17. 012 - Student #145 ==> Table #02
  18. 013 - Student #062 ==> Table #02
  19. 014 - Student #031 ==> Table #02
  20. *********************************
  21. 015 - Student #049 ==> Table #03
  22. 016 - Student #178 ==> Table #03
  23. 017 - Student #088 ==> Table #03
  24. 018 - Student #166 ==> Table #03
  25. 019 - Student #131 ==> Table #03
  26. 020 - Student #109 ==> Table #03
  27. 021 - Student #018 ==> Table #03
  28. *********************************
  29. 022 - Student #192 ==> Table #04
  30. 023 - Student #161 ==> Table #04
  31. 024 - Student #058 ==> Table #04
  32. 025 - Student #132 ==> Table #04
  33. 026 - Student #194 ==> Table #04
  34. 027 - Student #234 ==> Table #04
  35. 028 - Student #185 ==> Table #04
  36. *********************************
  37. 029 - Student #203 ==> Table #05
  38. 030 - Student #180 ==> Table #05
  39. 031 - Student #138 ==> Table #05
  40. 032 - Student #022 ==> Table #05
  41. 033 - Student #237 ==> Table #05
  42. 034 - Student #247 ==> Table #05
  43. 035 - Student #099 ==> Table #05
  44. *********************************
  45. 036 - Student #239 ==> Table #06
  46. 037 - Student #213 ==> Table #06
  47. 038 - Student #143 ==> Table #06
  48. 039 - Student #125 ==> Table #06
  49. 040 - Student #054 ==> Table #06
  50.  
  51. 'Output initentially omitted
  52.  
  53. *********************************
  54. 246 - Student #041 ==> Table #36
  55. 247 - Student #177 ==> Table #36
  56. 248 - Student #038 ==> Table #36
  57. 249 - Student #068 ==> Table #36
  58. 250 - Student #121 ==> Table #36
  59. *********************************
Oct 8 '11 #5
I can but simply say THANK YOU. This is exactly what I needed. I am indeed greatly humbled by the fact that you have provided the code and its output. P. Arre
Oct 9 '11 #6
ADezii
8,834 Expert 8TB
You are quite welcome. You already got the ball rolling, I just gave it a little push.
Oct 9 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
2
by: deanfamily | last post by:
I have a double-linked list. I need to insert RANDOM numbers into it. The rand() function doesn't put "random" number in the list, in the sense that every time you run the program it should...
14
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up...
2
by: eastband | last post by:
This is for homework... I need to generate a list of random numbers on command ========================================================== ? random 5 Result: ...
5
by: dav3 | last post by:
I have almost completed a monster assignment on sorting algorithms (quick, insertion and selection) using c++ but I am lost on one part of the assignment. I have to generate a random list of numbers...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
14
Deathwing
by: Deathwing | last post by:
Hi everyone, I'm fairly new with python and as such am playing around alot with just basic coding/scripting. I've spent the better part of today working with random numbers. Thanks for all your...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.