• C

random numbers

I need codes  for the generation of;

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

Can you help?
learnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
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
learnAuthor Commented:
Hi ozo,

Thank you.

Can you give whole program especialy the head files?
0
ozoCommented:
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

learnAuthor Commented:
Hi ozo,

I believe that your comment is an excellent answer!

Can you tell me what is RAND_MAX?
0
ozoCommented:
The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX
0
learnAuthor Commented:
Can I just use rand() instead of uniform(0,1)?
0
ozoCommented:
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
learnAuthor Commented:
Sorry, ozo, I stil not understand why should use RAND_MAX :-(
Is rand() a random number between 0 and 1? RAND_MAX = ?
0
ozoCommented:
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
mbormannCommented:
Please see
http://www.math.keio.ac.jp/~matumoto/emt.html

Mersenne Twister Home Page
(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.
The algorithm is coded into a C source downloadable below.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
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
learnAuthor Commented:
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
mbormannCommented:
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
learnAuthor Commented:
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".

If not, please tell me.

Regards.
0
hongjunCommented:
That is a wise method to solve the problem.
0
mbormannCommented:
thanks
0
MoondancerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.