Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I wish to Pick a number randomly from a large set of Inputs.Please Suggest the Logic in C#.Net
Posted

Well, supposing you have a way to individually address your inputs (i.e. an index) then you may use the Random class methods. The documentation, you know, contains some examples MSDN: Random.Next Method".
:)
 
Share this answer
 
If your input values are indexed i.e. you can access one value out of it using some index, then use methods in Random class (one which lets you set the upper and the lower limit).

In which format do you have your input values?
 
Share this answer
 
To add to the other answers, since you will already have the input numbers you should put them into a collection like a List<T> or Array. Then generate a random number using Random.Next(int) that is bound by 0 and the size of your collection minus 1. Use the result as an index for your collection. The code should look something like this:

double GetRandomElement(IList<double> items, Random rand)
{   
   int index = rand.Next((int)(items.Count()-1));
   return items[index];
}


You should be able to send an Array or List<T> to this method.
 
Share this answer
 
v7
Hi,

Please see the following code to generate random number in C#.

C#
Random RandomValue = new Random();<br />
       int value = RandomValue.Next(10000, 1000000);



Note:RandomValue.Next(10000, 1000000); contains two arhument one is Lower Limit and other is Upper Limit.
for the above example it will generate random number between 10000 to 1000000.

If this article is useful to you, please provide rank :)
Thanks & Regards,
Imdadhusen
 
Share this answer
 
use this and you will get the desired result

<br />
Random rand = new Random();<br />
<br />
Response.Write(rand.Next(0, 10)); <br />


where in rand.next("lower value","upper value")
 
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