For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > December 2005 > Random Number Generator









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Random Number Generator
morty3e@gmail.com

2005-12-14, 4:00 am

I have a small program that generates some random numbers using
rand_r(). I need numbers from 100000-FFFFFF. The program that I have
generates numbers fairly successfully (with a high number of threads, I
do get some number repeats), but it doesn't populate the enitre range
of digits that I need. At best it gives me up to XXXX and I have to
add 00 at the end to get it to be 6 digits. Is there a better method
for doing this that is thread safe (I will be running a high number of
threads)? I'm running on Solaris 8.

Thanks.


Code:
/**/
#include <sys/types.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

void moo(void * x);
void generateNUM(char *instr, int len);

int main(int argc, char *argv[])
{
int m;
pthread_t theThread;
for(m=0;m<1000;m++)
{
if (pthread_create(&theThread, NULL, (void *)moo, NULL)!= 0)
{
perror("pthread_create");
exit(1);
}
}
/* Wait a while then exit: all existing threads will die */
/* pthread_delay_np (&maintime); */
sleep(20);
return 0;
}

void moo(void * x)
{
char num[6]="";
int i;
struct timespec interval = {1, 0};

for(i=0;i<10;i++)
{
memset(num, '\0', 6);
generateNUM(num, 6);
printf("v%d=%s\n", pthread_self(), lniata);
nanosleep(&interval, NULL);
}
}

void generateNUM(char *num, int len)
{
struct timeval tval;
hrtime_t tmVal;
double dval;
unsigned int seed;
int randVal;
void *vptr=NULL;

//gettimeofday(&tval, NULL);
tmVal=gethrtime();
//dval=(tval.tv_usec);
//seed=(unsigned int)dval;
seed=(unsigned int)tmVal;
randVal = rand_r(&seed);
sprintf(num, "%x",randVal);
while((vptr=memchr(num, '\0', 6))!=NULL)
{
memset(vptr, '0', 1);
}
return;
}

Jordan Abel

2005-12-14, 4:00 am

On 2005-12-14, morty3e@gmail.com <morty3e@gmail.com> wrote:
> I have a small program that generates some random numbers using
> rand_r(). I need numbers from 100000-FFFFFF. The program that I have
> generates numbers fairly successfully (with a high number of threads, I
> do get some number repeats), but it doesn't populate the enitre range
> of digits that I need. At best it gives me up to XXXX and I have to
> add 00 at the end to get it to be 6 digits. Is there a better method
> for doing this that is thread safe (I will be running a high number of
> threads)? I'm running on Solaris 8.


Call the function again to populate the extra digits.

If your implementation uses the "classic" algorithm for random number
generation, it will generate numbers in the range 0:0x7FFF

rand() is probably thread-safe, though you might get the same number in
two threads that call it at [roughly] the same time, and of course your
random number sequence will not be reproducible.

why 100000 as a lower limit instead of 0?
Jordan Abel

2005-12-14, 4:00 am

Previously:
> If your implementation uses the "classic" algorithm for random number
> generation, it will generate numbers in the range 0:0x7FFF


but, of course, check your system's RAND_MAX.
Andrei Voropaev

2005-12-14, 4:00 am

On 2005-12-14, morty3e@gmail.com <morty3e@gmail.com> wrote:
> I have a small program that generates some random numbers using
> rand_r(). I need numbers from 100000-FFFFFF. The program that I have
> generates numbers fairly successfully (with a high number of threads, I
> do get some number repeats), but it doesn't populate the enitre range
> of digits that I need. At best it gives me up to XXXX and I have to
> add 00 at the end to get it to be 6 digits. Is there a better method
> for doing this that is thread safe (I will be running a high number of
> threads)? I'm running on Solaris 8.
>

[...]

On my linux installation info libc recommends using erand48 etc. for
generation of random numbers. The same doc says that rand_r although
POSIX compliant is "badly designed and unsuitable for serious work."


--
Minds, like parachutes, function best when open
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com