Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
this is the code random gen in integer, right?
C#
Random random = new Random();
int irandom = random.Next(1, 100);

what about for random gen in double?
from 0.0 to 100.0

and how make an array (25 to 50) random numbers?
Posted
Comments
Sergey Alexandrovich Kryukov 25-Oct-11 1:19am    
How about reading (very short) MSDN help page in Random, too hard, asking a question is easier?
--SA

You can use NextDouble or Sample and add it to your random integer.
Ref: http://msdn.microsoft.com/en-us/library/system.random.nextdouble.aspx[^]

You can generate n number of randoms and put it in array or whatever. Try it.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-Oct-11 1:20am    
Sure, a 5.
--SA
Prerak Patel 25-Oct-11 3:01am    
Thanks.
CPallini 25-Oct-11 4:08am    
I believe the proper way to do it is scaling and offsetting the random double to match the requested range. However, I guess it doesn't matter for a uniform distribution (like the one of the PC pseudo-random generator).
get a random number between 0 and 1000 and then map it to 0 to 100 to get a random number between 1 and 100 with one decimal point precision.

C#
public static double getrand()
{
    Random random = new Random();

    return random.Next(0, 1000)/10.0;
}
 
Share this answer
 
v2
Comments
André Kraak 25-Oct-11 4:48am    
My reason for 1 vote:
As mentioned in solution 1 the Random Class[^] has the method NextDouble[^].
Amir Mahfoozi 25-Oct-11 6:14am    
I didn't know that we should always respect to solution 1 here!
Thank you for reminding me.

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