Srandom number

I dont get how SRand works xD what would the code be for a simple 0 to 1? the tutorial here is confusing me
Something like this:
1
2
3
4
5
6
7
8
#include <iostream>
#include <ctime>
int main(int argc, char argv[])
	{
	srand(time(NULL));
	std::cout << rand() % 2;
	return 0;
	}
The psuedo random number generator generates a fixed sequence of numbers for a given seed value.

So if you start with the same seed value for your program, you always get the same sequence. To randomise the seed value, it's often seeded with some component of the current time, as that will change between program runs as in the example above. However, if you want to debug a program that uses random numbers, it's helpful to debug it using the same seed value to make the run deterministic.

srand() sets the seed value
rand() returns the next number in the sequence

rand() returns a value between zero and (2^32) - 1 (or 64 on a 64 bit system). Of you want a value between [0, 1) you need to convert the rand() value to a double in your range.
Topic archived. No new replies allowed.