![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
Can anyone explain Random number generation
Hi all,
How are random numbers generated , even using some functions supplied by the programming language? whats the logic behind the random number generation algorithm? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
This is the Knuth PRNG (Pseudo-Random Number generator) that was used in glibc3.1 for Linux, it is reentrant:
int
rand_r (unsigned int *seed)
{
unsigned int next = *seed;
int result;
next *= 1103515245;
next += 12345;
result = (unsigned int) (next / 65536) % 2048;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= (unsigned int) (next / 65536) % 1024;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= (unsigned int) (next / 65536) % 1024;
*seed = next;
return result;
} |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
short version:
Random numbers aren't really random. They are either "seeded" by a number or by the system time. If seeded by a number, they use calculations to come up with the "random" numbers. If seeded by the system time, they use that to come up with the random numbers. The system time method is better in some cases because it is closer to true randomness. |
|
|
|
|
|
#5 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
I was clearly referring to 'random numbers' in C programming. Does that clear it up, or am I still incorrect?
|
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
DaWei's point is that it is possible with the right hardware and libraries to get true random number in C. So just because we are talking about C and random numbers does not mean they are always pseudo-random.
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You are apparently referring to the PRNG commonly found in the run-time library. This does not preclude one from using C with true random numbers. I'm of the opinion that disseminating flakey information, whether by loose semantics or otherwise, is not a good idea. Again, I'd suggest you follow the earlier link for additional considerations involved in seeding the PRNG with the system's time.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
Can you show me such a code? Not saying it doesn't exist, but it seems that any code would not be truly random.
"I'm of the opinion that disseminating flakey information, whether by loose semantics or otherwise, is not a good idea." edit: DaWei, I'm all for correct grammar as well, but couldn't you just tell me to shut the hell up if I don't know what I'm talking about? ![]() Also: I already read everything in the original link. I'm still of the opinion that the numbers you say are "truly random" are only more significantly random than others. While I understand the article's point about distribution, a perfect distribution does not necessarily indicate true randomness. Is that incorrect? |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I didn't mention grammar.
The statement was made that C can use processes that generate truly random numbers. No one mentioned that this will happen with any PRNG, regardless of it's distribution. My view on misinformation remains the same; my expression seemed preferable, at the time of that post, to telling you to just shut the fuck up, read the posts, and maybe resort to Google.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Median/Mode in arrays? {Need help} | Java|Tera | Java | 27 | Nov 29th, 2005 11:50 AM |
| FiveDigit + RandomeNumber Game. | TecBrain | Java | 0 | Nov 18th, 2005 3:53 PM |
| Random Number | coldDeath | Python | 1 | Sep 11th, 2005 9:17 AM |
| Random Number & Average Problem | Hadrurus | Java | 6 | Aug 15th, 2005 2:08 PM |
| non repeating random number generation | gencor45 | C# | 2 | Feb 9th, 2005 1:11 AM |