Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Does anyone have a random number generator in c# similar to the functions "randn" and rand in matlab
Posted
Comments
Dr.Walt Fair, PE 5-Aug-11 13:38pm    
Is there a problem with using the normal one in C#?
Ravi-original 5-Aug-11 14:47pm    
The normal C# random number generator does not generate according to any distribution such as normal distribution as done by "randn" function in matlab. I want a random number generator which generates according a distribution which we assign to the random number
Sir.Ixildore 5-Aug-11 15:11pm    
I was looking at randn and it seems it produces an array of random number depending on how you define the scope of the result(max, min, size of the array, and seed). Then it shouldn't be a random number generator you are looking for, instead, an array of random numbers generator.

A simple way go get a normally distributed random number:

- accumulate N uniformly distributed numbers, say in range 0..R.

- substract the mean, NR/2.

- divide by the standard deviation, Sqrt(NR^2/12).

The larger N, the better the approximation. Use N=10.

Also see http://en.wikipedia.org/wiki/Box-Muller_transformation[^].
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 6-Aug-11 3:35am    
My 5. It's wonderful: only you paid attention for the requirement of normal distribution; but all the votes got the first answer completely ignoring the requirement.
--SA
YvesDaoust 6-Aug-11 5:19am    
Well, in this question one letter was important: the 'n' at the end of 'rand'. I wouldn't be surprised most of them missed it.

Thanks for the comment.

- Yves
Ravi-original 3-Jan-12 11:15am    
You are absolutely correct. Everyone missed it. Thanks
Ravi-original 3-Jan-12 11:16am    
Your method might just work but I found and validated a library that worked really well
Try as below.

C#
Random random = new Random();
int randomNumber = random.Next(0, 100);
 
Share this answer
 
Comments
Espen Harlinn 5-Aug-11 13:42pm    
My 5
RaisKazi 5-Aug-11 13:42pm    
Thanks Espen.
Alexandru Ghiondea 5-Aug-11 14:00pm    
The way the current solution is wrote, the same numbers will be generated every-time you run your app.

You should provide a seed to the Random number generator. I usually use (int)DateTime.Now.Ticks.

Thanks,
Alex
Wjousts 5-Aug-11 14:30pm    
No it won't. Read the docs:

Initializes a new instance of the Random class, using a time-dependent default seed value.

The parameterless constructor basically does what you suggest internally.
Alexandru Ghiondea 5-Aug-11 14:31pm    
You are correct! I was not aware of this - thanks! :)

Alex
Random.Next generates a random number whose value ranges from zero to less than Int32.MaxValue
Reference Link :- Random.Next Method[^]
have a look at CP-[Simple Random Number Generation][^] article which enable you to learn some basics of Random number generation.
 
Share this answer
 
If your concern is that the Random class is time dependent/seed dependent and calling .Next() multiple times may have resulted in the same value then you must have created your object in the method scope. Try having a reference of the Random object in the class scope or if you really want to have that object locally you can delay the call to the next .Next() call by putting delays/sleep etc. You should have a pretty decent random number results. You can refer to this [MSDN link]
 
Share this answer
 
I found a library by metanumeric which generates random numbers based on the distribution we set e.g. normal, exponential etc.

This is the link for the library.
http://metanumerics.codeplex.com/[^]

worked really well for me.
 
Share this answer
 
You may also equate the cumulative of the random number extracted with the Random.Next to the cumulative of the normal distribution and then compute corresponding normal random number (yes you have to do some numeric computations).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900