playMaker

Author Topic: How would you create a set of Random Ints with no Repeats?  (Read 1686 times)

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Basically I want to by able to generate 3 random Ints between 0 and 5, with no numbers repeating. So for example <3,2,4> and <4,0,1> are acceptable, but not <2,2,1> or <0,0,0>. No numbers should come up more than once. How would I do this?

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: How would you create a set of Random Ints with no Repeats?
« Reply #1 on: May 04, 2018, 12:53:52 AM »
There are a few ways to do this. They require using a simply array (a list of ints in this case). I think the easiest way is to do a shuffle array.

Make an array with all the numbers you want (say 1 - 5). Then use the shuffle array to mix them up. The just get them and they will be random. (And only include each number once).

Or, you can use the ' random int' . But each time you get a number, you need to add it to an array. And next time you generate another random, you need to check that array to see if you already have it.

Shuffle seems more straightforward to me.


Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: How would you create a set of Random Ints with no Repeats?
« Reply #2 on: May 04, 2018, 03:54:34 PM »
Yeah tcmeric had basically covered it.

If youre not fammiliar with arrays or do not want to use them, you could just create 3 int variable and just check that the second one you make is not the same as the last one (or last 2). This will be the clumsy long winded way of doing it though haha.

So:

3 variables: int1, int2, int3

First state: Set int1 to random int. Move on.

Second state: set int2 to random int, check if same as int1 or different. If same, Move on to state 3 if different move on to state 4.

state 3: set int2 to random int., check if same as int1 or different. If same, Move back to state 2 if different move on to state 4.

State 4: set int3 to a random int, check if int3 is same as int1. If same, Move on to state 5 if different move on to state 6.

State 5: set int3 to random int., check if same as int1 or different. If same, Move back to state 4 if different move on to state 6.

State 6 check if int3 is same as int2. if same move back to state 4, if different move on to state 7.

State 7: do whatever you want to do with your 3 ints.

This is of the top of my head but shoud work i believe. Although tcmeric had a much cleaner solution, I thought I would still pitch in with the clumsy noob way of doing it.

Good luck with it!

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Re: How would you create a set of Random Ints with no Repeats?
« Reply #3 on: May 05, 2018, 01:16:04 AM »

Make an array with all the numbers you want (say 1 - 5). Then use the shuffle array to mix them up. The just get them and they will be random. (And only include each number once).


I tried this one. Worked perfectly, thanks. I would assume though that it would get annoying with large sets of numbers (since you'd need to make a "Number array" with all of them), but it works for what I'm trying to do.