The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
generating 16 digit random number in C ajaysahoo High Level Programming 10 08-25-2009 12:12 PM
Generating random numbers phil_heath Shell Programming and Scripting 9 08-06-2009 10:52 PM
Generating random number within a specific range (0.5-1.5) TehOne Shell Programming and Scripting 10 04-05-2009 07:25 PM
Generating line number Amey Joshi UNIX for Dummies Questions & Answers 5 07-09-2008 06:12 AM
generating random numbers with hamming distance 4 hack_tom Shell Programming and Scripting 2 04-29-2007 02:27 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-06-2009
trey85stang trey85stang is offline
Registered User
  
 

Join Date: May 2008
Posts: 70
C Help; generating a random number.

Im new to C, and Im having a hard time getting a random number.

In bash, I would do something similar to the following to get a random number;

Code:
#!/bin/bash
seed1=$RANDOM
seed2=$RANDOM
seed3=$RANDOM
SEED=`expr $seed1 * $seed2 / $seed3`

echo ${SEED%.*}
Now, in online examples for C everyone suggest seeding srandom with time, so I attempted seeding with time_t now. Which works great except this program will run probably 20-30 times a second... to seeding with time_t now wont work.. as I'll continually get the same random number.

How can a randomize better in c? IS there a way to get milliseconds or nanoseconds with time.h? I dont care if it's not 100% accurate I just need to figure out how to get a truly random number.
  #2 (permalink)  
Old 10-07-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,762
Do not reseed. Otherwise you keep getting the same value.

PNG's are setup to generate a series of random numbers from a single starting point. Reseeding before every call to rand() "undoes" that. IT does NOT randomize better.

From your 'truly random' statement I'm getting the idea that you may not know too much about random numbers generators and their limitations. Random means that you have an equal chance of getting any number, including the one you just got before. For rand() that means you are supposed to have an equal chance for any number 1 - RAND_MAX. rand() is not a great PNG.

Try lrand48(). It is somewhat better and is there on most all unix boxes.
-- Seed it only once.

Do not use any of these functions for cryptographic applications.
  #3 (permalink)  
Old 10-07-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,937
Quote:
Originally Posted by jim mcnamara View Post
Do not reseed. Otherwise you keep getting the same value.

PNG's are setup to generate a series of random numbers from a single starting point. Reseeding before every call to rand() "undoes" that. IT does NOT randomize better.
...but he should seed *once* -- or he'll get the same numbers anyway. I take "running 30 times per second" to mean 30 processes per second, and each process must seed once to get different numbers.

Yes, you can get seconds and microseconds from gettimeofday().
  #4 (permalink)  
Old 10-09-2009
Zykl0n-B Zykl0n-B is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 25
As Corona688 says,
If you use the same value to generate a "random" value, the algorithm is always going to perform the same operation with the same number, so, you need to parse values which are always going to be different, and yes, with gettimeofday() you can parse system time in nanoseconds to get a different number every nanosecond.

Try this:

Code:
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main(void){
int i;
unsigned int value;
struct timeval tv;
struct timezone tz;
struct tm *tm;
tm = localtime(&tv.tv_sec);

for(i = 0;i < 30; i++){
gettimeofday(&tv, &tz);
value = rand_r(&tv.tv_usec);
printf("%i\n", value);
}
return(0);
}
  #5 (permalink)  
Old 10-09-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,762
The OP is using time(). RANDOM (in bash) doesn't call rand() it calls random() I believe.
My response was geared to using rand()

The best choice is either urandom() or random(). And yes you do seed once. But gettimeofday on a multiprocessor/multicore box with threads can have the same effect as calling time() - the same seed value. I saw that years ago on a 8-cpu HPUX PARISC box.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0