My question is straightforward. I know abour srand and rand, I know how to seed my random generator and so on. I am specifically interested in the mathematics behind srand. How does the computer use let's say 3333 as a seed and calculates a "random" number?
Asked
Active
Viewed 84 times
0
-
5There are quite a few examples of simple pseudo-random number generators on the Internet, if you just search a little. But to be able to understand and write a *good* generator takes a lot of studying and experience, including plenty of math and statistics. – Some programmer dude Sep 15 '20 at 11:44
-
i dont understand the question, when you call `srand` then you pass the seed as parameter. Do you want to know how the first random number is generated from that seed? – 463035818_is_not_an_ai Sep 15 '20 at 11:46
-
Let's take the example with srand(time). Here the first number ALWAYS follows the UNIX time which makes sense. The other numbers are generated by using the previous number as a seed. But question is just how does srand take a seed and calculates the next "random" number? Computers never do "random" things but can use matemathics to perfom what would seem like "randomness". – Sep 15 '20 at 22:31
-
Ps. Sorry about my formalia on Stack, I am completly new here, and need to study a bit to find out how to highlight things and so on((: – Sep 15 '20 at 22:32
2 Answers
4
The c standard library's and therefore the c++ standard library's std::rand
implementation is up to the library implementation. It is not mandated by the standard. The following function
(source: https://xkcd.com/221/)
would be a perfectly valid implementation for it.
That is the reason why it should under no circumstances be used any more. Instead defer to the well-defined, implementation independent and portable mersenne twister.
-
1
-
1@idclev463035818 Well if it's any consolation, that usually happens to me :) – bitmask Sep 15 '20 at 11:49
-
Yes but such an implementation would be a terrible mess. Indeed random but nothing is more deterministic than a constant value. So I'm not sure anyone would call such an implementation "valid" in practice (even if it does not violate the standard). – Fareanor Sep 15 '20 at 11:49
-
[Another related comic](https://dilbert.com/strip/2001-10-25) – Some programmer dude Sep 15 '20 at 11:52
-
@Fareanor Of course not. But the point is that given that the standard *allows* this, means that pretty much any implementation (deterministic depending on seed) would conform. Which makes `rand` pretty much useless for any practical application. – bitmask Sep 15 '20 at 11:52
-
1@Fareanor I think part of the joke is that unless you know how it is implemented you cannot tell the difference between this and a real prng – 463035818_is_not_an_ai Sep 15 '20 at 11:53
-
1@Someprogrammerdude Perhaps, but I wouldn't be thrilled to be associated with Scott Adams or his work in any way. – bitmask Sep 15 '20 at 11:56
-
@bitmask I fully agree with that, I posted my comment before your edit _"That is the reason..."_ :) – Fareanor Sep 15 '20 at 11:57
0
It depends on the library implementation but in general it would be enough to calculate a well mixed hash of the argument.

Christopher Yeleighton
- 454
- 2
- 6