- Joined
- Apr 17, 2018
- Messages
- 4
Last edited:
But Rm and randomnumber 2 can be equal right ?And it is a loop right ?If the range of numbers is small. make an unordered list of numbers.
RandomNumbers[0] = 1
RandomNumbers[1] = 2
RandomNumbers[2] = 3
RandomNumbers[3] = 4
RandomNumbersLength = 4
Set RandomNumberSelection = random number between 0 and (RandomNumbersLength - 1)
Set Rm = RandomNumbers[RandomNumberSelection]
Set RandomNumbersLength = (RandomNumbersLength - 1)
Set RandomNumbers[RandomNumberSelection] = RandomNumbers[RandomNumbersLength]
Say Rm is 2, a random number roll of 1 in the range 0...3, then the list will look like...
RandomNumbers[0] = 1
RandomNumbers[1] = 4
RandomNumbers[2] = 3
RandomNumbersLength = 3
The process can be repeated for randomnumber2.
Set RandomNumberSelection = random number between 0 and (RandomNumbersLength - 1)
Set randomnumber2 = RandomNumbers[RandomNumberSelection]
Set RandomNumbersLength = (RandomNumbersLength - 1)
Set RandomNumbers[RandomNumberSelection] = RandomNumbers[RandomNumbersLength]
Say randomnumber2 is 1, a random roll of 0 in the range 0...2, then the list will look like...
RandomNumbers[0] = 3
RandomNumbers[1] = 4
RandomNumbersLength = 2
This process can get another 2 unique random numbers, after which an exception case must occur as there are no more unique random numbers to choose from.
I recommend this approach over the above loop approaches as it will always execute with constant time complexity.
No.But Rm and randomnumber 2 can be equal right ?
BloodSoul explains in more detail, but basically it is because it pulls and removes random numbers from a pool of random numbers. If you want to think of a real life equivalent, it is like how a raffle works where tickets are pulled from a hat and the same ticket cannot be pulled twice because they are removed from the hat as they are pulled.Can you explain it to me why Rm and randomnumber2 wont be equal if it is so ?
Now i understand.No.
Well, DSG made a list, and included the numbers from 1 to 4 in it. The idea is every time you get a number from that list, you have to remove it so that you won't get it again next time.
As you can see, the list has 4 entries (indices from 0 to 3, and each respectively stores): 1, 2, 3, and 4.
Set RandomNumbers[0] = 1
Set RandomNumbers[1] = 2
Set RandomNumbers[2] = 3
Set RandomNumbers[3] = 4
Set RandomNumbersLength = 4
Now, we tell a function to get a random number based on the existing indices - initially from 0 to 3.
Let's assume that we got the number 1, and that particular index holds the number 2.
After getting our number, remove it from the list. Here comes the tricky part: the easiest way to do so is copying the last index's value, which is 4, and replace the chosen index (RND -> 1). Also, remember that "RandomNumbersLength" indicates the size of our list, we must subtract it by 1 now.
Set RND = RandomInt(0, 3) -> 1
Set result = RandomNumbers[RND -> 1] -> 2
That's basically it - We got our number (result), and reduced the list size. The number 2 is not going to be picked again, because it got replaced by the number 4 (last index). After you're done with it, and need to do the same process, don't forget to reset the list to how it was before.
Set RandomNumbersLength = (RandomNumbersLength - 1) -> our list now has 3 entries.
-------- However, we didn't do anything yet to replace the index 1. Do it now --------
Set RandomNumbers[RND -> 1] = RandomNumbers[RandomNumbersLength -> 3] -> number 4
Reminder: RND variable should be set like this
Set RND = (Random integer number between 0 and (RandomNumbersLength - 1))
Thanks a lot for the method.BloodSoul made me to understand it.BloodSoul explains in more detail, but basically it is because it pulls and removes random numbers from a pool of random numbers. If you want to think of a real life equivalent, it is like how a raffle works where tickets are pulled from a hat and the same ticket cannot be pulled twice because they are removed from the hat as they are pulled.