Amazon Interview Question
Software Engineer / Developersint randv(unsigned int max)
{
if(seed==-1)
{
seed=(unsigned int) time(NULL);
}
unsigned int nc= time(NULL);
unsigned int mix=(nc+seed)%MAX_INT;
seed=mix;
return mix%max;
}
This should generate the same number. try coding and see. you need a different seed everytime.
better to use system time to seed
I think in C++, new operator will allocate on heap, which is randomly. We can use this property to generate a random number.
For example, you can try this
int *p = new int(5);
cout << &p << endl;
every time, the address is different and random.
try reading from one of these two files /dev/random or /dev/urandom
- Anonymous March 10, 2011not using any builtin function ;)