Does windows have a random number function?
Printable View
Does windows have a random number function?
Don't think so, but the standard library does:
rand returns a number between 0 and RAND_MAX.Code:#include <iostream>
#include <cstdlib>
using namespace std;
void main() {
cout << rand() << endl;
}
or this
Code:#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
cout<<1*rand()%6<<endl;
return 0;
}
How do i get one between 0 and 99?
on mine you would do 0*rand()%99
parksies I think you would do this
#include <iostream>
#include <cstdlib>
using namespace std;
void main() {
RAND_MAX = 99
cout << rand() << endl;
}
RAND_MAX is already defined -- use:
Code:float myrand(float lower, float higher) {
return (((float)rand() / (float)RAND_MAX) * (higher-lower)) + lower);
}
Thanks, but both those return the same sequence of numbers each time.
Seed the random number using srand at the start of your program, using the time as stated earlier.
It STILL gives the same sequence of numbers.