Problem with the random

Good day. I have a problem with the random. Suppose the number 3 was dropped. The next time the number 3 again dropped out. How to get rid of the repetition?

Hi,

If you just want to get random number different than the previous one than this should work (or similar):

newRandom = oldRandom
while (newRandom == oldRandom)
{
oldRandom = newRandom
newRandom = getRandom()
}

If you want to get random numbers e.g. from 1 to 100 (without repetition) then create an array of 100, fill it with numbers from 1 to 100 and then just “mix” the elements in array. Now just go from the first element to the last one everytime you want the next “random” number.

Best,
kds

1 Like

Why don’t you use mersenne random generator?

try
int nums[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::random_shuffle(nums, nums + 9);

Working! Thanks for the help!