Random Number Generator
-
Hi, I have an array (which may change in length each time) of numbers (employeeid) and I want to create a random generator function to return one these numbers. The only parameters I will be receiving are the array of numbers and the array length. I'm new to C# so any sample code would be appreciated. Thanks.
-
Hi, I have an array (which may change in length each time) of numbers (employeeid) and I want to create a random generator function to return one these numbers. The only parameters I will be receiving are the array of numbers and the array length. I'm new to C# so any sample code would be appreciated. Thanks.
public int GetRandomInt(int[] array, int length)
{
return array[new Random.Next(0, length)];
} -
public int GetRandomInt(int[] array, int length)
{
return array[new Random.Next(0, length)];
}Thanks for your reply. I'm getting the follwoing error.. Error 2 'System.Random.Next()' is a 'method' but is used like a 'type' All I require is just one number returned from the array. Does return array not return a list of arrays or does the Next method refine it to one number? Pardon my ignorance.
-
Thanks for your reply. I'm getting the follwoing error.. Error 2 'System.Random.Next()' is a 'method' but is used like a 'type' All I require is just one number returned from the array. Does return array not return a list of arrays or does the Next method refine it to one number? Pardon my ignorance.
Belfast Child wrote:
Does return array not return a list of arrays or does the Next method refine it to one number?
array is your variable that was passed to the method. Using the square brackets is indexing the array to retrieve one of the members of the array. Since it is an array of int, you will return an int, which is what the method signature specified.
-
Thanks for your reply. I'm getting the follwoing error.. Error 2 'System.Random.Next()' is a 'method' but is used like a 'type' All I require is just one number returned from the array. Does return array not return a list of arrays or does the Next method refine it to one number? Pardon my ignorance.
-
Belfast Child wrote:
Does return array not return a list of arrays or does the Next method refine it to one number?
array is your variable that was passed to the method. Using the square brackets is indexing the array to retrieve one of the members of the array. Since it is an array of int, you will return an int, which is what the method signature specified.
Cheers Guys. Figured it out. Here's a test on a click button for anyone else interested
public int GetRandomInt(int[] array, int length) { Random ranNum = new Random(); return array[ranNum.Next(0, length)]; } private void button1_Click(object sender, EventArgs e) { int [] intArray; intArray = new int[3] {123, 321, 666}; int randumNum = GetRandomInt(intArray, 3); randomLabel.Text = (randumNum.ToString()); }
-
Thanks for your reply. I'm getting the follwoing error.. Error 2 'System.Random.Next()' is a 'method' but is used like a 'type' All I require is just one number returned from the array. Does return array not return a list of arrays or does the Next method refine it to one number? Pardon my ignorance.
You have to put parantheses on the constructor, i.e. use "new Random()" instead of just "new Random". It's better to create the Random object once, and use that throughout the program, than to create a new one for every random number. When you create a new object, it will be seeded from the system clock, so if you create them too closely in time, the randomness will be poor. Also it's a waste of resources to create more than one Random object.
--- b { font-weight: normal; }
-
Cheers Guys. Figured it out. Here's a test on a click button for anyone else interested
public int GetRandomInt(int[] array, int length) { Random ranNum = new Random(); return array[ranNum.Next(0, length)]; } private void button1_Click(object sender, EventArgs e) { int [] intArray; intArray = new int[3] {123, 321, 666}; int randumNum = GetRandomInt(intArray, 3); randomLabel.Text = (randumNum.ToString()); }
You should use
ranNum.Next(length)
instead ofranNum.Next(0, length)
as it produces random numbers within the same range and is almost 2 times faster. Also I would suggest to retrieve the array length dynamically instead of using a fix value. Either do it when you call theGetRandomInt
method or even remove the length parameter and do it inside the method.int randumNum = GetRandomInt(intArray, intArray.Length);
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook