TripleX Random Int

It’s not a particularly important question but I am curious as to how the Random works in terms of how it actually generates the number. I ask this because if I run my Code it returns the same number as the Lecturer and if I cancel out in the terminal and rerun I get the same numbers again.

Why are the numbers the same when they are generated using the random function, wouldn’t a random function result in us getting completely different numbers to each other as they are well random.

Because it’s being seeded with the same number. If you don’t call srand at the start of your program then that has the effect of being seeded with srand(1).

The deck of cards analogy is probably the best way to understand it. You have a random sequence of numbers, and seeding it is shuffling the deck of cards in a specific way. Then every call to rand() will read the next card in the pile and putting it face down into another pile when you run out of cards in the original pile you flip the other pile and start again.

But instead of a pile of 52 cards it’s a pile of at least 32767 and numbers might appear more than once.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms