rand () and srand () .can anyone explain. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

rand () and srand () .can anyone explain.

rand (). cstdlib

9th Dec 2016, 10:01 AM
F.Heeh. 2nd Yr. Hebron University‏‎
F.Heeh. 2nd Yr. Hebron University‏‎ - avatar
4 Answers
+ 5
To generated random number you need to use rand() function and srand() function use to initialization of rand. Put your srand above if statement. And initialize rand by srand(time(NULL)). You have to use ctime header file for time function. it would work perfect. if you not initialized rand then, it will generate same number. if you want it within a range use rand()%(max-min+1)+min Hope it helps you
9th Dec 2016, 10:10 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 5
thanks for the help I understand now
9th Dec 2016, 11:23 AM
F.Heeh. 2nd Yr. Hebron University‏‎
F.Heeh. 2nd Yr. Hebron University‏‎ - avatar
0
check out my code fun with true random nos , I ve included lots of helping comments, it will help
9th Dec 2016, 10:21 AM
Morpheus
Morpheus - avatar
0
there are 2 types of random no. generator RNGs 1. Pseudo RNGs , that don't need any value and generate random no. s using 2 things * cstdlib library & *rand() function but it's called pseudo coz it's less random in nature, can statistically determine the no.s 2. True RNGs, that need a seed value to generate best possible random no.s, hence called true it uses 3 things *cstdlib- the library that stores the functions *srand(seed_value)- function to get any seed value, in my code example it's the age value. rand()- to generate random no.s
9th Dec 2016, 10:35 AM
Morpheus
Morpheus - avatar