Solved

# random numbers

Posted on 1999-11-20
Medium Priority
365 Views
I need codes  for the generation of;

[1] Normally distributed random numbers
[2] Exponentially distributed random numbers.

Can you help?
0
Question by:learn
• 6
• 6
• 3
• +2

LVL 85

Expert Comment

ID: 2222116
do(
v1 = uniform(-1,1);
v2 = uniform(-1,1);
rsq = v1*v1+v2*v2;
}while( rsq >= 1.0 || rsq == 0.0 );
fac=sqrt(-2.0*log(req)/rsq);
normal1 = v1*fac;
normal2 = v1*fac;

exponential = -log(1-uniform(0,1));
0

Author Comment

ID: 2222211
Hi ozo,

Thank you.

Can you give whole program especialy the head files?
0

LVL 85

Expert Comment

ID: 2222485
Whole program to do what with the random numbers?
I'll assume print them..

[1]
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#define uniform(a,b)    ((double)rand()*((b)-(a))/RAND_MAX+(a))
main(){
double v1,v2,rsq,fac,normal1,normal2;
srand(time(0));
while( 1 ){
do{
v1 = uniform(-1,1);
v2 = uniform(-1,1);
rsq = v1*v1+v2*v2;
}while( rsq >= 1.0 || rsq == 0.0 );
fac=sqrt(-2.0*log(rsq)/rsq);
normal1 = v1*fac;
normal2 = v2*fac;
printf("%f\n%f\n",normal1,normal2);
}
}

[2]
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#define uniform(a,b)    ((double)rand()*((b)-(a))/RAND_MAX+(a))
main(){
double v,exponential;
srand(time(0));
while( 1 ){
do{
v = uniform(0,1);
}while( v == 0 );
exponential = -log(v);
printf("%f\n",exponential);
}
}
0

Author Comment

ID: 2222557
Hi ozo,

Can you tell me what is RAND_MAX?
0

LVL 85

Expert Comment

ID: 2222566
The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX
0

Author Comment

ID: 2222579
Can I just use rand() instead of uniform(0,1)?
0

LVL 85

Expert Comment

ID: 2222595
You can just use (double)rand()/RAND_MAX instead of uniform(0,1)
If your system has drand48(), which is usually more random than rand(), you can just use it instead of uniform(0,1)
0

Author Comment

ID: 2222601
Sorry, ozo, I stil not understand why should use RAND_MAX :-(
Is rand() a random number between 0 and 1? RAND_MAX = ?
0

LVL 85

Expert Comment

ID: 2222658
As I said, rand() is a random integer in the range 0 to RAND_MAX
The value of RAND_MAX macro shall be at least 32767
0

LVL 5

Accepted Solution

mbormann earned 240 total points
ID: 2222897
http://www.math.keio.ac.jp/~matumoto/emt.html

(A random number generator)
Four times faster than rand()
Of period 2**19937-1

I have used the Java version for doing some financial calculations and it's really good ,please try it out

What is Mersenne Twister?

Mersenne Twister(MT) is a pseudorandom number generator developped by Makoto Matsumoto and Takuji Nishimura (alphabetical order) during 1996-1997. MT has the following merits:

It is designed with consideration on the flaws of various existing generators.

Far longer period and far higher order of equidistribution than any other implemented generators. (It is proved that the period is 2^19937-1, and 623-dimensional equidistribution property is assured.)

Fast generation. (Although it depends on the system, it is reported that MT is sometimes faster than the standard ANSI-C library in a system with pipeline and cache memory.)

Efficient use of the memory. (The implemented C-code mt19937.c consumes only 624 words of working area.)

Regards
Amit
0

LVL 85

Expert Comment

ID: 2222935
Yes, that's a better pseudorandom number generator than many implementations of rand(), as I suggested in Q.10106514 Q.10224466
So you could replace uniform(0,1) with genrand()/4294967296.0
0

Author Comment

ID: 2223391
To mbormann:

Thank you very much for your help. That looks like a new and better way to genarate random numbers. I have visited the site holding some free code, a great site.

Now, I have another problem:

In one of my comments, I have promised to zoz. I tald him: "I believe that your comment is an excellent answer!" since he give me not only the genaration of the random numbers of the "first level", but also the "second level". I mean the "normally distributed" and "Exponentially distributed" level by the whole programs.

What I should do :-)
0

LVL 5

Expert Comment

ID: 2223460
ask the person who u feel helped u more to lock this question and if u want it post a dummy question for the other.

Mersenne is very fast random number generator isn't it?
:-)
0

Author Comment

ID: 2223500
To ozo:

You see...the problem :-)

If you agree, I will publish another question titled "Q for ozo to answer" for you to get the 60 points. That will be a Q about what is the numger, 4294967296.0, in "genrand()/4294967296.0".

Regards.
0

LVL 33

Expert Comment

ID: 2223995
That is a wise method to solve the problem.
0

LVL 5

Expert Comment

ID: 2230430
thanks
0

LVL 1

Expert Comment

ID: 6821504
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects that and the expert will now receive these points, all verified.

Please click on your Member Profile, select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.

Thanks,
Moondancer
Moderator @ Experts Exchange
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.