+ 2
Friends how to generate a random number without using random function in c language or c++
5 Antworten
+ 6
This is how rand and srand were implemented back in 1989:
unsigned int next = 1;
int rand()
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed;
}
Hope this helps.
+ 3
Vlad Serbu just a curiosity... is there any specific reason or logic of choosing those number in your function?? I got that 32768 is int limit and mod by that number will ensure num to be in int range... for other numbers, I am not getting...
+ 2
thank you Vlad service😇
+ 2
Ketan Lalcheta They are chosen so as to create a uniform distribution, but other then that I can't offer much info I'm afraid.
+ 1
Also, see https://en.m.wikipedia.org/wiki/Pseudorandom_number_generator and https://en.m.wikipedia.org/wiki/Hardware_random_number_generator for a more in-depth view of random numbers.