#include <iostream> #include <cstdlib> int main(int argc, char *argv[]) { int seed = 10; srand(seed); std::cout<<"Rand() return an integer between 0 and "<<RAND_MAX<<std::endl; //generate random integer number for(int i = 0; i < 5; i++) std::cout<<rand()%100<<std::endl; //generate random float number [0, 1] for(int i = 0; i < 5; i++) std::cout<<rand()/float(RAND_MAX)<<std::endl; return 0; }
#include <iostream> #include <cstdlib> #include <ctime> int main(int argc, char *argv[]) { int seed = 10; srand(time(NULL)); std::cout<<"Rand() return an integer between 0 and "<<RAND_MAX<<std::endl; //generate random integer number for(int i = 0; i < 5; i++) std::cout<<rand()%100<<std::endl; //generate random float number [0, 1] for(int i = 0; i < 5; i++) std::cout<<rand()/float(RAND_MAX)<<std::endl; return 0; }