Solved

Random number generation

Posted on 2002-05-22
23
308 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
unable to delete all specified values regedit 38 702
dog bark java program 15 119
sumHeights2  challenge 7 130
Problem to Office 1 39
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

713 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