Question about erand48 usage


 
Thread Tools Search this Thread
Top Forums Programming Question about erand48 usage
# 1  
Old 10-15-2010
Question about erand48 usage

Hi,

Following is the trimmed down version of the code I'm using to generate a set of random numbers using erand48

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main()
{
        unsigned short xsubi[3];
        xsubi[0]=10;
        double r;
        int x[400];
        int i=0, a=0, b=150;
        r=erand48(xsubi);
        x[i]=floor(a + (b - a + 1)*r);
        printf("%f\n", r); 


}

I expect the code to print the same value for r every time, but it prints a different value! If I remove the line where the floor is calculated then I get the same value of r every time I run the code. Why should this happen? Does reading r somehow change something?

Thanks in advance for any insights you may have.
# 2  
Old 10-15-2010
I get the same output every time I run it, but I'm not certain that's what you mean. I also note you're not seeding the RNG anywhere, so you shouldn't expect the RNG to be at any particular value on program startup. And I have no idea why you're putting the result of floor into the array like that, which will of course modify what values you get out of erand since that's where it stores its state!
# 3  
Old 10-15-2010
It is common, in testing, to seed random generators with a constant for repeatability, and then in later testing and production, seed them with time() or fine time like from gettimeofday() or the like to make them vary randomly run by run.
# 4  
Old 10-15-2010
Of course it is. I don't see that he's doing so.
# 5  
Old 10-15-2010
What happens if you move
Code:
       unsigned short xsubi[3];

outside of main()?
This User Gave Thanks to fpmurphy For This Post:
# 6  
Old 10-15-2010
erand48(3) - Linux man page

Well, since you are not seeding it, it might be more consistently unseeded if it was not on the stack picking up junk from prior calls. I think heap might be zeroed out.

Maybe just seed it with a constant or something from the env. for testing, and then seed it with time for more randomness, if desired. You need 6 bytes, and gettimeofday() provides 32 bit signed int time() plus 0-999999 microseconds, which feels like 41 bits.
# 7  
Old 10-15-2010
@fpmurphy - Moving
Code:
unsigned short xsubi[3];

outside solves my problem!

Thanks for all replies. This is the first time i'm using erand48, but after reading the man page I felt that you do not need to call any of the seed functions and in effect you set the seed by initializing the Xi value. Doing
Code:
xsubi[0] = 10

I thought was achieving that, may be not filling all of the 48 bits, was an issue. I also tested this by initializing xsubi[0] to a different value and then I would get a new set of random numbers. All of this is what I wanted except that putting the floor call then somehow stopped giving me a consistent set of random numbers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed command usage question

How to work x in sed command? I know x command is swaps the contents of pattern space and hold space. But i am unable to understand it's working? (4 Replies)
Discussion started by: Vartika18
4 Replies

2. UNIX for Dummies Questions & Answers

Question on disk size and usage

Hello All, this may be a simple question but I wasn't sure how to word it to search for the answer. Have a look at the following: /dev/mapper/vgdata-lvdata 626G 594G 0 100% /data01 As you can see the disk size 626GB of which 594G is used. There's 32GB difference there but the... (5 Replies)
Discussion started by: bbbngowc
5 Replies

3. UNIX for Dummies Questions & Answers

Ln usage question

Is it possible to assign a symbolic link to a network folder, without that folder being mounted? Thanks! (1 Reply)
Discussion started by: nerdcurious
1 Replies

4. Shell Programming and Scripting

Question regarding sed usage

I have a html file with the following content:- <font face=verdana color=#000000>108946</font> <font face=verdana color=#000000>234346</font> I want to format the values inside the font tag using thousand separator. I have the following command which can be used for adding thousand... (4 Replies)
Discussion started by: Yoda
4 Replies

5. UNIX for Dummies Questions & Answers

rsync usage question

Hello folks; I'm using rsync on my Ubuntu servers to sync a remote folders on a remote machine to a local folders on a local machine. one thing i couldn't get to work is how to exclude folder. I know i'm suppose to use "exclude" argument but every time i do, i still see the excluded directory... (0 Replies)
Discussion started by: Katkota
0 Replies

6. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

7. Shell Programming and Scripting

usage of sed question for experts

I need a little help with sed. Basically, I need to parse out selections from the output of hddtemp so conky can display some hdd temps for me. I have hddtemp in daemon mode so A simple 'nc localhost 7634' displays the following: $ nc localhost 7634... (3 Replies)
Discussion started by: audiophile
3 Replies

8. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

9. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

10. AIX

basic question about disk usage

how to i find out the disk usage on a server. say in windows examples its like C:/ D:/ and checking out the disk space. how can i find in Unix. can i just use df -k (3 Replies)
Discussion started by: karthikosu
3 Replies
Login or Register to Ask a Question