Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
15th Sep 2017, 6:57 AM
Юрий Исаев
Юрий Исаев - avatar
+ 28
You need to use srand() to generate new random nos everytime you run your code.. Using rand() will only create one random no, and each rime you run the code.. it will give the output as that no only...
15th Sep 2017, 6:44 AM
Frost
Frost - avatar
15th Sep 2017, 6:32 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 10
The rand () function generates random numbers based on a value called "seed" if you don't change the seed you will always get the same number. to change the seed constantly you could use the ctime library and write srand (time (NULL)); at the start of main
15th Sep 2017, 6:14 PM
Marco
Marco - avatar
+ 9
add library <ctime> and function srand(time(0));
15th Sep 2017, 6:51 AM
Юрий Исаев
Юрий Исаев - avatar
16th Sep 2017, 10:36 AM
Chob
Chob - avatar
+ 4
do not use rand, use #include <random> and then use random_device rd; mt19937 gen (rd ()); uniform_int_distribution dist (min, max); then you assign the vaule of the dist to an int and you get a real random number. search for #include <random> and then read up on it on msdn
16th Sep 2017, 1:07 PM
Axel Hornay
Axel Hornay - avatar
+ 3
The Answer to the Main Question of Life (meaning of life)? :-) (For downvoters: Have you noticed how many times number "42" appears in lessons of Sololearn's courses? Do you know why? 😉)
16th Sep 2017, 12:47 AM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
+ 2
https://code.sololearn.com/cYBlZmG9WEo8/?ref=app what you are probably using and https://code.sololearn.com/c9JTKEGIAluM/?ref=app real random numbers (they use the current time as the seed to create the next random number)
17th Sep 2017, 7:18 PM
Nick
Nick - avatar
+ 2
Seed it with the time. Oldest trick on the book. I don't have the book though.
18th Sep 2017, 1:47 AM
Arthur Tripp
Arthur Tripp - avatar
+ 1
42 is the answer to the meaning of life, the universe and everything, so your code works like a charm. ;-)
17th Sep 2017, 10:55 PM
John Fytros
John Fytros - avatar
0
You sure? Usually it outputs 41.
15th Sep 2017, 3:21 PM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
0
That is because rand() don't give you an actual random number srand() gives you that
15th Sep 2017, 6:30 PM
Muhammad Aliyu
Muhammad Aliyu - avatar
0
random function actually generates random numbers by a seeding unit. if seeding number is not entered computer uses default seeding to give same random number everytime. to generate different random number you need to use seeding function srand().
16th Sep 2017, 9:33 PM
MUNEEB AHMAD DAR
MUNEEB AHMAD DAR - avatar
0
you have to use srand()
17th Sep 2017, 9:10 AM
dinesh dine
dinesh dine - avatar
- 4
how to make a best WordPress website
17th Sep 2017, 6:29 AM
RADHESHYAM GOHEL
RADHESHYAM GOHEL - avatar