cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Random number generator
Random number generator
Apr 19, 2012 at 5:43am UTC
nilagayu
(8)
I need to generate binary random numbers. But whatever i do the code seems to generate the same random number for every execution. I need the code to generate different random numbers for each run.
i used 3 different methods but none helped
1.cout<< bitset<32>(char( rand() ) ) <<"\n"
2. for(int i=0;i<32;i++)
{
r=rand() %2+0;
r1[i]=(char)((int)'0'+r);
}
3. for(int i=0;i<32;i++)
{
r=rand()/((RAND_MAX+1)/2);
r1[i]=(char)((int)'0'+r);
}
Could someone help me with this?
Apr 19, 2012 at 5:59am UTC
Disch
(13742)
you need to seed the random number generator.
Call srand once at the start of main:
srand( (
unsigned
)time(0) );
Apr 19, 2012 at 6:48am UTC
nilagayu
(8)
Thanks a lot. That solved the problem
Topic archived. No new replies allowed.