+ 2

Friends how to generate a random number without using random function in c language or c++

18th Aug 2018, 12:54 PM
Programmer Raja
Programmer Raja - avatar
5 ответов
+ 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.
18th Aug 2018, 1:38 PM
Vlad Serbu
Vlad Serbu - avatar
+ 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...
18th Aug 2018, 3:46 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
thank you Vlad service😇
18th Aug 2018, 3:29 PM
Programmer Raja
Programmer Raja - avatar
+ 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.
18th Aug 2018, 7:27 PM
Vlad Serbu
Vlad Serbu - avatar
18th Aug 2018, 3:35 PM
Vlad Serbu
Vlad Serbu - avatar