Solved

Random number generation

Posted on 2002-05-22
23
300 Views
Last Modified: 2013-11-20
Hi,

I am trying to generate a sequence of random floating points.
The mean of this sequence should be around 0 and the standard deviation should be around 1.
This sequence should consist of negative and positive numbers.
I used rand() to generate random numbers and convert it to floating point by dividing it by 10000. Then I randomly assign them to be positive and negative. I do not have any idea of how to make these sequence of numbers to have a mean of 0 and standard deviation of 1. Does anyone knows how? Please help.

Thank you
xstar

0
Comment
Question by:xstar
  • 10
  • 9
  • 4
23 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 7027194
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7027413
2 xstar
try this
(rand()*2.0/RAND_MAX) - 1.0
0
 
LVL 6

Expert Comment

by:snoegler
ID: 7028072
2 AlexNek:

"... standard deviation of 1 ..."
Are you sure rand() guarantees that?
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7028337
2 snoegler
rand() is not the best solution. You can have (for N at least 20000):
average = 0 -+0.004
sd = 1 -+ 0.002
r = (rand()*3.47/RAND_MAX) - 1.739;

I only want to show an easy transformation.
0
 

Author Comment

by:xstar
ID: 7029429
Hi Alexnek,

i tried your method
r = (rand()*3.47/RAND_MAX) - 1.739;
and it works! it is a simple equation but it does a good job.
it generates a sequence of numbers whose mean and standard deviation(sd) close to 0 and 1. I tried it with a few different seeds and found that the mean and sd are quite consistent.
but how do you get 3.47 and 1.739?
will i get the mean and sd close to 0 and 1 for all the random numbers generated by this equation? i mean, what is the pecentage of accuracy that it will get this mean and sd.

Thank You
xstar
0
 
LVL 16

Accepted Solution

by:
AlexNek earned 200 total points
ID: 7030644
Hi xstar,
>will i get the mean and sd close to 0 and 1 for all the random numbers...
I don't know exact percentage of accuracy for rand function. It is about 0.xx for the mean and 0.xxx for sd.
Take a look at:
http://www.santafe.edu/projects/swarm/swarmdocs/userbook/swarm.random.sgml.appendix.html

>but how do you get 3.47 and 1.739?
did you know which kind of transformation is y=2*z*f(x)-z?if f(x)={0..1}.  --> f(x) = {-z..+z}

f(x) = rand()/RAND_MAX;
By sample: m =0.501 and sd = 0.2884
Then from sd(f(x,z)) = 1, I can find z (1.7337).
0
 

Author Comment

by:xstar
ID: 7050239
Hi Alexnek,

I have tested the equation r = (rand()*3.47/RAND_MAX) - 1.739 and found that the random numbers criteria i need is more than this.
Have you use matlab before?
its nrand() produces a sequence of number that have a mean of 0 and standard deviation of 1. I found that the sequence of numbers it generate falls between -4 and 1. the negative and positive numbers of the sequence is also quite proportionate. but for the mean and sd, its not as accurate as your equation. its mean and sd is arround -0.1xxx and  0.7xxx.
I am not sure of the what criteria the random numbers must meet so i have to try out a few random number generateor in order to get the most appropriate one. after testing out matlab random numbers, i found that the sequence it generated allows my program to works more accurately. how do i add on to your equation to meet the "criteria". Please help.
the matlab function could be found in this link
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.shtml

Thank You
xstar
0
 
LVL 6

Expert Comment

by:snoegler
ID: 7050617
Hi xstar,

have you already taken a look at the link i posted?

Cite: "The results are uniformly distributed, unbiased, and unpredictable unless you know the seed. "
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7050667
Hi xstar,
I don't use Mathlab and I haven't it. Do you know exact link with demo version?
It is possibly that you can give me needed numbers without Mathlab installation for me.
I need: random function name, generated range, mean and sd for the normalized range 0..1 (for about 100000)
From your post I can get only: nrand(), range -4..1 (very strange range, for normalization use (nrand()+4)/5, please)

PS:I hope nrand give us normal distribution.

0
 

Author Comment

by:xstar
ID: 7050916
Hi,

AlexNek

sorry for not being able to provide the necessary criteria i need because i am not sure of it myself!

I guess i need some more time to do more testing on the program that takes in these random numbers. There might also be another portion of the program that might be wrong. as i have no means of change that portion, i want to make sure that the random number generator is correct.

Right now, the criteria for this random number generator i can provide is :
1) It is to have a mean of 0.
2) standard deviation of 1.
3) proportiationate positive and negative float.

but my tests shows that this might not be a very accurate criteria as, the matlab rand() function produces a sd(0.7xxx) less accurate than the equation(r = (rand()*3.47/RAND_MAX) - 1.739 ), but is able to produce a more stable result.
test out 3 different seeds and found that matlab randn() generates random numbers between -4 to 1.

so what i would like to try now is to get a set of random numbers close to matlab randn() function.

i think i would do more testing on the matlab randn() and the equation before proceeding with this "no direction" random testing.

If you all have more comments/suggestions, please post them up. your help is greatly appreciated.

Thank You
xstar
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7052131
2 xstar
I have read Matlab documentation, you can choose from two functions:
The rand function generates arrays of random numbers whose elements are uniformly distributed in the interval (0,1).

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/rand.shtml#998306

The randn function generates arrays of random numbers whose elements are normally distributed with mean 0, variance 1 , and standard deviation 1.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randn.shtml

If you start to use C functions rand() you need to use mathlab rand() functions too. Try this (rand()*3.47) - 1.739.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:xstar
ID: 7053422
Hi,

snoegler,
i have went to the website you posted.
as i will need to do a lot of testing for every different generator, i think i will need some time to try the generator.
but is the generator faster than rand()?

alexnek,

sorry, i didn't get what you mean.
if i use (rand()*3.47) - 1.739, would it generate very big numbers?
and how do you use c and matlab at the same time?

thank you
xstar
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7054973
2 xstar
>how do you use c and matlab at the same time?
No, not together, Matlab has function rand too!!
This formula for Mathlab rand function.

Possible I don't understood you. First, you needed random numbers for C, second, you wrote that you need it for Mathlab too.
For what purpose you need random numbers?
Where you want to use it?
0
 

Author Comment

by:xstar
ID: 7055188
Hi,

i don't need random numbers for matlab. its just that the random numbers generated from matlab seems to be the type of numbers i need. i'm working on c++, so i still need a c random generator. therefore i thought that there might be a c generator that works like the matlab randn() function.

sorry for this confusion. i think i will take some more time to test out r = (rand()*3.47/RAND_MAX) - 1.739. this  might be all i needed.

thank you
xstar
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7055827
Hi xstar
>like the matlab randn()
Please pay attention to Mathlab randn function generate normally distributed numbers and C function rand uniformly distributed.
0
 

Author Comment

by:xstar
ID: 7061426
Hi,

I think what i need is a normally distributed random numbers with a mean of 0 and standard deviation of 1.

Thank you
xstar
0
 
LVL 6

Expert Comment

by:snoegler
ID: 7061437
If you've got uniformly distributed random numbers (i.e. ISAAC for example), you can transform that to an gaussian (i.e. normal) distribution. Sample:

http://www.csse.monash.edu.au/~lloyd/tildeMML/Continuous/Normal.html

(The page also shows using a Javascript to perform that transformation)
0
 

Author Comment

by:xstar
ID: 7061518
Hi,

I think what i need is a normally distributed random numbers with a mean of 0 and standard deviation of 1.

Thank you
xstar
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7062123
Hi xstar
you can try the next code: (it is only from theory I not tested it)

double function GetGaussNumber()
{
  double z;   //your result

  z = 0.0;
  for(int i = 0; i < 12; i++){
    z += rand()/RAND_MAX;
  }
  return z - 6;
}
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7063514
corrected version: "Average 0.00346, deviation 0.99709"
double GetGaussNumber()
{
     double z;   //your result

     z = 0.0;
     for(int i = 0; i < 12; i++) {
          z += ((double)rand()/RAND_MAX);
     }
     return z - 6.0;
}
0
 

Author Comment

by:xstar
ID: 7071728
Hi,

Alexnek, i think you are correct about the c random generator. From the numbers it generated, i think it is uniformly distributed.
I had found a link to a random number generator which is normally distributed at http://www.agner.org/random/.
If you have any other comments about this random generated, please post it up.
Thanks for all the help.

xstar
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7072308
Hi xstar,
thank you, I glad to help you.
The link is very good, I can found a lot of descriptions. But about which comments are you interested?
0
 

Author Comment

by:xstar
ID: 7074488
maybe about the randomness, speed, what kind of problems it might cause.
for the normal distributed function, i could also find it at the website http://www.taygeta.com/random/gaussian.html.

thanks
xstar
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 150
deburging in oracle form 12 76
ORA-01403: no data found 43 82
viewing source code from eclipse 13 74
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now