Generating random integers (without repeats)


#1

Hi,

Does anybody have any thoughts on how might I generate a set of 5 integers without repeats?

Thanks!


#2

You could generate the smallest integer and then make the second integer the sum of that number and another randomly generated integer and just continue on until you get 5 integers.


#3

Thanks, Jay. I should have been a bit more specific.

Am looking for a way to generate 5 integers within a specific interval… let’s say between 1 & 100.
I think you’re method would work, but I’m not sure how to make sure they stay less than 100.

Also, I would like all numbers to have an equal chance of being chosen, so
r.int(1,20), int(21,40), int(41,60), etc… won’t work.

Thanks in advance


#4

I am not sure if there is a better solution, but here is a solution.

r1 generates a number (1…100).

We then set r2 to be anything but r1. We set the random generator bounds to (r1+1, r1+99). Then use mod 100 to find the equivalent.

Then it gets a bit trickier because (1) you will have two separate domains, (2) you have to account for either r1 or r2 being the minimum, and (3) you will need to make a random generator to choose between the domains.

I start my domains with 1 above the min random to 1 less then the max random. Then I loop around from 1 above the max to 1 below the min (using mod)

You would need to continue this process for r4 and r5 which would have 3 domains and 4 domains respectively. You would then have a random generator to select between the 2 domains of r3, a generator for the 3 domains of r4, and a generator for the 4 domains of r5.

Depending on your needs you could always make a “pseudo random” generator using prime numbers and mod.

I stopped about half-way through because I started to look for other methods.

https://teacher.desmos.com/activitybuilder/custom/5c0b32b84f73a96c94809903


#5

I finished the previous activity, but ran into a few problems at first:
(a) Zeroes were showing up
(b) 100 wouldn’t show up
( c) Randomness was effected because domains were chosen with equal weight instead of number of elements in the domain.
(d) Sometimes the uniqueness would have an error particularly over small domains or when consecutive integers were selected.

I reduced the problem to finding 5 unique integers [1,5] and was able to debug it all then set it back to 100. You can change U_1 in the graph component for however many integers.

I also turned it into a random poker hand generator. It would also be pretty easy to rig it for other images of random selection for probability (e.g. dice, coins, marbles, etc.)

https://teacher.desmos.com/activitybuilder/custom/5c0c0660dbdefd757cc8f645


#6

Vince,

Thank you so much. I think I understand your plan based on your first post and it makes sense. I’ll be diving into your code soon.

Thanks for your time & effort!


#7

Hi Vince,
Very nice work here. I haven’t had time to completely dissect (I admit I got lost halfway though)… but I put a table in and I noticed that after a few button presses, sometimes an integer higher than the upper bound will appear.

Spent a prep period and quite some time after to brute force it using another method. I tried generating a random integer, and if it matches one of the previous integers, it takes a new random integer, etc… (I put 9 iterations). With 9 iterations, (if upper bound is high enough), there should be a very low chance of a repeated number after 9 random generations… so I hope it’s OK.

I put my work in slide 2 of yours: Random Generator (no repeats) • Activity Builder by Desmos

Lmk if you have any suggestions or thoughts (or maybe I misunderstood your slide).

Thanks again, Vince.


#8

Hey Richard,

I understand getting lost in the work, it’s definitely not third party friendly at the moment. I probably need to add many more explanatory notes.

I made edits so that the table references I1…I5 instead of r1…r5. I also saw the notation you used in slide 2, so I added the parameters L1 and U1 in the component of the graph which equate to the lower bound and upper bound (I haven’t checked if lower bounds other than 1 are working). I also removed the cards attached to the points.

https://teacher.desmos.com/activitybuilder/custom/5c0fda9c1666650ab1c3c450

Note*
r1, r2, r3, r4 r5, do go beyond the upper limit because they essentially extend the numberline. In essence, if r1 is 88, then r2 will pull from the next 99 numbers (i.e. from 89 to 187). This is passed through the mod function, so that it selects numbers 89-> 87 because mod( r2,100) will treat 101-187 as 1-87. The variables defined in terms of mod I named I1…I5

Let me know if you have any specific questions about the code. Carrying out this solution was a bit more complex than I originally expected. However, I appreciate you posting the question here because it has led to some useful simulation activities with my students. Always finding new ways to use Desmos to enhance instruction!