Random number help

  • Thread starter Thread starter nomad
  • Start date Start date
N

nomad

Hi,

I need to be able to retrieve a random number from an array of
numbers. I know there is a Random method in the framework but it
doesn't seem to deal with an aray of numbers. Any help would be much
appreciated.

Thanks
 
nomad said:
Hi,

I need to be able to retrieve a random number from an array of
numbers. I know there is a Random method in the framework but it
doesn't seem to deal with an aray of numbers. Any help would be much
appreciated.

Thanks

You need to fill an array with a loop calling one of the Random.Next
methods.
 
Random rand = new Random();


public static T randomElement(T[] arrayToDrawFrom)
{
//get a random element from an array
return arrayToDrawFrom[rand.Next(arrayToDrawFrom.Length)];
}
 
rossum said:
I need to be able to retrieve a random number from an array of
numbers. I know there is a Random method in the framework but it
doesn't seem to deal with an aray of numbers. Any help would be much
appreciated.
Use the Random.Next method to pick a random integer between 0 and the
size of the array:

public int PickRandom(int[] array) {
Random rand = new Random();
return array[rand.Next(array.Length)];
}

If you are going to be doing a lot of picking then move the
declaration of rand up to class level so you are not redeclaring it
every time you invoke the method.

For performance reasons that should be done for a real lot of picking.

For randomness reasons that should be done for more than one picking.

Arne
 
nomad said:
I need to be able to retrieve a random number from an array of
numbers. I know there is a Random method in the framework but it
doesn't seem to deal with an aray of numbers.

Other has shown you how to index into the array with
a random number.

But there is one important question: do you want to remove
the picked element so it can not be picked again?

Arne
 
Other has shown you how to index into the array with
a random number.

But there is one important question: do you want to remove
the picked element so it can not be picked again?

Arne

THank you all for your replies. I only need to select a random number
from the array without having to remove that number afterwards so the
above solution would be perfect.

THanks again.
 
For performance reasons that should be done for a real lot of picking.

For randomness reasons that should be done for more than one picking.

Out of curiosity, should your second sentence have a "not" in there, or is
it correct as stated?
 
Jeff said:
Out of curiosity, should your second sentence have a "not" in there, or is
it correct as stated?

You do not want to create a new random number generator more than once,
as often the sequence is based on the clock time at the start of
generation. If you call new Random() inside a quick loop, you often end
up with repeated values, because the seed is the same.
 
Jeff said:
Out of curiosity, should your second sentence have a "not" in there, or is
it correct as stated?

I think it is correct as stated.

To achieve randomness it should be moved out as soon as you will pick
more than one random number.

No harm in always moving it out so that would be good habit.

Arne
 
Back
Top