0

C language : How to generate random Number in every launch of program?

It's pretty difficult for me to do that... I tried the rand fonction but nothing... Any help?

4th Dec 2020, 7:19 PM
Thunder Jack
Thunder Jack - avatar
2 Answers
+ 7
Using the rand() function is just one side of the coin: https://en.cppreference.com/w/c/numeric/random/rand You also need to seed it with a start value before using it, otherwise you will get the same results. This is done via srand(): https://en.cppreference.com/w/c/numeric/random/srand A typical value for srand() is generated with the help of the time() function, i.e. srand( time ( 0 ) ); This is necessary because pseudo-random number generation is basically just a numerical algorithm; if you start with the same number, you get the exact same result. Therefore, you need a variable seed to get different numbers every launch.
4th Dec 2020, 7:48 PM
Shadow
Shadow - avatar
+ 1
I see... Thanks for the tip. I'll try it
4th Dec 2020, 8:54 PM
Thunder Jack
Thunder Jack - avatar