[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

Random number generation

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
xstar
Asked:
xstar
  • 10
  • 9
  • 4
1 Solution
 
snoeglerCommented:
0
 
AlexNekCommented:
2 xstar
try this
(rand()*2.0/RAND_MAX) - 1.0
0
 
snoeglerCommented:
2 AlexNek:

"... standard deviation of 1 ..."
Are you sure rand() guarantees that?
0
Technology Partners: 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!

 
AlexNekCommented:
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
 
xstarAuthor Commented:
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
 
AlexNekCommented:
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
 
xstarAuthor Commented:
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
 
snoeglerCommented:
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
 
AlexNekCommented:
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
 
xstarAuthor Commented:
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
 
AlexNekCommented:
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
 
xstarAuthor Commented:
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
 
AlexNekCommented:
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
 
xstarAuthor Commented:
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
 
AlexNekCommented:
Hi xstar
>like the matlab randn()
Please pay attention to Mathlab randn function generate normally distributed numbers and C function rand uniformly distributed.
0
 
xstarAuthor Commented:
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
 
snoeglerCommented:
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
 
xstarAuthor Commented:
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
 
AlexNekCommented:
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
 
AlexNekCommented:
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
 
xstarAuthor Commented:
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
 
AlexNekCommented:
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
 
xstarAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 10
  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now