The following code snippet shows how to generate a random number between a range in C#, where min and max are the minimum and maximum range of the new number.
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
How to use it?
Copy the above code in your class where you want to use it and call it like this.
int returnValue = RandomNumber(5, 20);

Manoj PrabhuPosted Jul 24, 2014, 12:04 AM
how i generate random number for 5 different cases....whether i have to use random() for every case or...
maryam saharPosted Apr 18, 2012, 1:07 PM
Hello I want to generate random processes to schedule them.how can i do this? what should be the output type? each process has 2 argumants : C(Cpu time per frame) and P(period). would you help me please?
maryam saharPosted Apr 18, 2012, 12:30 PM
Hello I want to generate Random processes to schedule them.what's the output type should be? each process has 2 argumans: C (CPU time per frame) and P (period) . would you plaese help me?
Gabriella FoxPosted Feb 20, 2012, 9:14 AM
Is there a way to sort a string of random numbers?
Miroslav BuckoPosted Aug 23, 2011, 11:39 AM
You should create instance of Random class outside the method “Why is this?” you ask. Well, the Random class actually generates pseudo random numbers, with the “seed” for the randomizer being the system time. If your loop is sufficiently fast, the system clock time will not appear different to the randomizer and each new instance of the Random class would start off with the same seed and give you the same pseudo random number.
fifi MunsonPosted Apr 30, 2011, 12:03 AM
That was exactly what I was looking for! Thanks a lot! ?
asad perwezPosted Apr 11, 2011, 1:40 PM
I used it but it doesnt work at all and it shows the error that the Next method does not have definition wht should be the probable error
PankaJ DPosted Jan 13, 2011, 7:35 AM
Random class belongs to which namespace?
Martijn van der KooijPosted Aug 26, 2010, 3:10 AM
Should we create a instance of Random seperately from it's use? Now a new Random with seed is started time after time. It will generate random numbers as long we do not repeat our call in to less time. I expect a static member of a class static Random random = new Random(); with the call from this class as: private int RandomNumber(int min, int max) { return random.Next(min, max); } Now we reuse this Random object several times and get a new number on each next....
bahare gPosted Aug 16, 2010, 1:19 AM
hi firends i tested random method.when i use this method in more than one times it can't generate random number any more. how can i use it correct to return me random numbers in a more than one time.
Anubhav SinghPosted Aug 12, 2010, 6:56 AM
gooddddddd
fafagirl tooPosted May 3, 2010, 5:26 PM
how can make prvent rndom function to produce the same two numbers?
rakesh kakkaraPosted Nov 28, 2008, 9:59 PM
code for generating non reapting random numbers
Mahesh ChandPosted Dec 27, 2006, 10:08 AM
Copy the above code in your class where you want to use it and call like this: int returnValue = RandomNumber(5, 20);
dewsPosted Dec 21, 2006, 2:44 PM
how do i use this?