Yes I know to use srand()
But does anyone have anything more random?
Thnx in advance
Printable View
Yes I know to use srand()
But does anyone have anything more random?
Thnx in advance
Thanks. I don't have I compiler on this computer so I can't try it out. Would this work:
That should give an integer between 1 and 100, right?Code:#include<iostream.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(NULL));
cout<<"A random number: "<<(int)((double)rand()/((double)RAND_MAX+1)*100)<<endl;
cin.get();
return 0;
}
I tried it, it's giving me 79 every time. 80 Now, so it's not random, it's 100% based on the time in which it's seeded. I'll check in a minute to see what's wrong.
Thanks. I would do it myself, but as I said before I don't have a compiler on this comp and I can't download one
This is working for me..
Although i'm sure you don't want the windows header in there... I can't figure out what's wrong with the regular time() implementation.Code:#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int main(int argc, char* argv[])
{
srand(GetTickCount());
cout<<"A random number: "
<< rand() / (RAND_MAX / 100 + 1)
<< endl;
cin.get();
return 0;
}
I would prefer not to have the windows header in my program. I'm trying to make this thing non platform specific. Anybody write their own random function?
I dont know what the problem with your system is but the ocde you posted works just fine on VC60.
maybe you had the srand(time(NULL)) in the loop so you kept re-seeding rand with the same value; make sure you only seed once
Yes, that would be a problem if you seeded everytime you were going to use rand();