a06e
a06e

Reputation: 20774

What's the seed used by random() if I don't initialize it with srandom()?

What's the seed used by C's random() if I don't initialize it with srandom()? Is it system time?

Upvotes: 0

Views: 90

Answers (3)

Memin
Memin

Reputation: 4090

ISO C Random Number Functions You can check here that If you call rand before a seed has been established with srand, it uses the value 1 as a default seed.

http://www.gnu.org/software/libc/manual/html_node/ISO-Random.html

Upvotes: 0

Emmet
Emmet

Reputation: 6411

As another answer correctly states, random() is not in ISO C. It is, however, in POSIX, where the rule is analogous to ISO C's rand():

Like rand(), random() shall produce by default a sequence of numbers that can be duplicated by calling srandom() with 1 as the seed.

Upvotes: 2

bluss
bluss

Reputation: 13772

random() is not in standard C.

ISO C defines rand() and srand().

#include <stdlib.h>
int rand(void);
void srand(unsigned int seed);

The standard answers your question in section 7.22.2:

If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

Upvotes: 5

Related Questions