Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Can anyone explain Random number generation (http://www.programmingforums.org/showthread.php?t=12710)

mutyalarao Mar 2nd, 2007 11:02 AM

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?

jim mcnamara Mar 2nd, 2007 11:12 AM

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;
}


Wizard1988 Mar 2nd, 2007 11:20 AM

One of our members has a good explanation :).
http://www.eternallyconfuzzled.com/a..._art_rand.aspx

Fall Back Son Mar 26th, 2007 1:47 AM

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.

DaWei Mar 26th, 2007 1:51 AM

Quote:

Random numbers aren't really random.
Yes, they are. PSEUDO-random numbers aren't really random. You might want to read the material provided in the preceding posts.

Fall Back Son Mar 26th, 2007 2:04 AM

I was clearly referring to 'random numbers' in C programming. Does that clear it up, or am I still incorrect?

Game_Ender Mar 26th, 2007 2:08 AM

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.

DaWei Mar 26th, 2007 2:10 AM

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.

Fall Back Son Mar 26th, 2007 2:16 AM

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?

DaWei Mar 26th, 2007 9:40 AM

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.


All times are GMT -5. The time now is 1:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC