LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-09-2004, 10:20 PM   #1
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
rand() question


I'm working on a small client/server game that will eventually have it's own random terrain. To make it so that the server doesn't have to send the whole landscape data to the clients, I was planning to use a random number generator and just have the server send the seed to the clients so that they can generate the landscape using the same set of random numbers.

Ultimately, I'll probably end up writing my own random number generator or using a 3rd party one, but I was just curious about something. Do the ANSI standards for C dictate what sort of formula is used for rand()? Specifically, if I use the same seed for srand() on various ANSI C compilers, am I guaranteed to get the same sequence of numbers from rand()?
 
Old 11-10-2004, 12:25 AM   #2
karlan
Member
 
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Rep: Reputation: 30
ansi standards DO NOT dictate how the function implements the internals. Random numbers sequences using any given seed will be different on Linux vs Windows, etc(most of the time). However, I could assume they could be the same if you are using GNU libraries on both systems.

Last edited by karlan; 11-10-2004 at 12:27 AM.
 
Old 11-10-2004, 08:20 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Original Poster
Rep: Reputation: 31
Thanks. That's what I suspected.
 
Old 11-10-2004, 05:19 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Get a copy of Knuth - the basic algorithm for most rand() implementations comes from there. It's a three liner in C.
The TYPE_0 linear congruential algorithm (rand() ) in GNU is:

Code:
   {
      int32_t val = state[0];
      val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
      state[0] = val;
      *result = val;
    }
state[0] is the seed value in the PNRG global state table - since rand() doesn't use a state table, it just uses state[0] for parking previous values. Just put this in your code and be done with it.
 
Old 11-11-2004, 08:18 AM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Original Poster
Rep: Reputation: 31
Thanks, Jim!
 
Old 11-11-2004, 12:18 PM   #6
phoenix7
Member
 
Registered: Aug 2004
Distribution: Mandrake 10.2(Mandriva), SuSE 9.3, Slackware 9.1, Xandros 3.1, Knoppix 3.9, FreeBSD 5.3, RedHat9
Posts: 122

Rep: Reputation: 15
I think srand & rand work with timer and don't do the same work.
 
Old 11-11-2004, 02:10 PM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Huh?
In the example above srand() sets the inital value of
state[0]

Programmers often call srand() with a large sequential, non-repeating number - the value returned by time(); - so that the sequence for rand always starts at a new point.
Code:
srand(time(NULL));
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
rand() and syntax error lmvent Programming 2 10-06-2005 12:21 PM
genrating -2, -1, 1, 2 woth rand() hubabuba Programming 9 08-14-2005 01:59 PM
srand(), rand(), RAND_MAX, or XXX weirdness nazdrowie Programming 2 03-07-2005 12:45 PM
Simple sh script: rand reorder Sinope Programming 2 09-11-2004 05:24 PM
Need help using rand() in C KneeLess Programming 12 10-01-2003 12:51 PM


All times are GMT -5. The time now is 10:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration