Generating random integers (without repeats)

Hi,

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

Thanks!

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.

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

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

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

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!

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.

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!

1 Like

This activity wasn’t found. Are you still using the original link? Definitely curious to see how this project continues!

Here’s a Copy & Edit version of what Vince had (I’m not sure whether this was the most updated). His work is in the first slide. Feel free to ignore my mess on the second.

1 Like

I know this is a pretty old post. If you consider going back to make this more third party friendly as you put it, you might want to consider using some functions instead of defining every variable. For example,
M_a(L_a,U_a)={L_a=0:0, abs(U_a-L_a)+2}
then M_3a=M_a(L_3a,U_3a)
M_4a=M_a(L_4a,U_4a), etc.

1 Like

Thanks for the suggestion. Unfortunately, I probably won’t go back to it anytime soon. I think when I first made that I did not realize that Desmos supported two-variable functions. I had only used CL for maybe a month at that point.

I used a different method, which is a bit more condensed and hopefully easier to follow.

1 Like

I’ve been looking for nice and efficient ways to do sampling without replacement, and what is above is great! I am just posting to see if others have other methods that can be used within activities?

Thanks in advance!

Something like this, where you could define s_eed in the CL:

L=unique( [1...1000].random(40,s_eed) )[1...10]

This would take the list [1…1000] and select 40 values (with replacement), reduce that list to only the unique values, and then select only elements 1-10. So 10 values “without” replacement.

3 Likes

The shuffle and random function that now exist in the GC make it a lot easier than before.

If I need 5 numbers between 1 and 52.

Ceil(52random(5)) is with replacement.

But without replacement. Just do a shuffle and index the first 5 in the shuffle.

4 Likes

The only issue with this method is that all students will receive the same set of five elements every time (i.e. graphs shuffle once and use those values every time). If you want different values for each student, a random number needs to be generated in the CL either as a seed or the actual randomized values.

I just tried out @HumanizedMath’s example and ran test activity and was able to get different elements for each student.

However, there is an issue in duplicating the screen. If I do, the same numbers on screen 1 show up on screen 2, albeit different for each student. The current workaround is to go into the graph of the second screen and press the reshuffle button.

3 Likes

They must have changed something with the calculator. Previously, anything randomly generated in the calculator was static, as opposed to values generated in the CL.
Good news. Shuffling was the first thing I tried in the past without success, and that was my workaround.