Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 2nd, 2007, 11:02 AM   #1
mutyalarao
Newbie
 
mutyalarao's Avatar
 
Join Date: Aug 2005
Location: Bangalore,India
Posts: 1
Rep Power: 0 mutyalarao is on a distinguished road
Send a message via Yahoo to mutyalarao
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?
__________________
Regards,
Mutyala Rao Aripaka,
Bangalore,
INDIA
mutyalarao@gmail.com
mutyalarao is offline   Reply With Quote
Old Mar 2nd, 2007, 11:12 AM   #2
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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;
}
jim mcnamara is offline   Reply With Quote
Old Mar 2nd, 2007, 11:20 AM   #3
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 423
Rep Power: 4 Wizard1988 is on a distinguished road
One of our members has a good explanation .
http://www.eternallyconfuzzled.com/a..._art_rand.aspx
__________________

Wizard1988 is offline   Reply With Quote
Old Mar 26th, 2007, 1:47 AM   #4
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 312
Rep Power: 3 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Mar 26th, 2007, 1:51 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
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
DaWei is offline   Reply With Quote
Old Mar 26th, 2007, 2:04 AM   #6
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 312
Rep Power: 3 Fall Back Son is on a distinguished road
I was clearly referring to 'random numbers' in C programming. Does that clear it up, or am I still incorrect?
Fall Back Son is offline   Reply With Quote
Old Mar 26th, 2007, 2:08 AM   #7
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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
Game_Ender is offline   Reply With Quote
Old Mar 26th, 2007, 2:10 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 26th, 2007, 2:16 AM   #9
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 312
Rep Power: 3 Fall Back Son is on a distinguished road
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?
Fall Back Son is offline   Reply With Quote
Old Mar 26th, 2007, 9:40 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:39 AM.

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