Math problem

  • Thread starter Thread starter Ignacio Machin \( .NET/ C# MVP \)
  • Start date Start date
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

What you can do is get the probables values in an array, then select a
randon from 0 .. array.length - 1

Cheers,
 
Here's a solution:

Create an array with the size of the number of dedired values. Populate the
array with the desired values, then find a random position in the array
(using the length of the array as part of the randomization).

How about that?
 
Hello

Something like this should work..

Random random = new Random();

int [] predef = {15,22,33,6,.....};//eg, 25 predefined mumbers

int index = random.Next(0,25);//get a random index

int num = predef[index];
 
Hi,

What you can do is get the probables values in an array, then select a
randon from 0 .. array.length - 1

Cheers,

If the values can be picked only once (ie a card deck) use an ArrayList
and remove the picked 'card'.
 
In addition, if the set is contiguous, then you can just use the
overload of Next on the random class and it will return a number in that
range for you.

Hope this helps.
 
Here's some code I just put together ..

int[ ] list = new int[5];

list[0] = 1;
list[1] = 23;
list[2] = 34;
list[3] = 45;
list[4] = 56;

int foundNumber = list[new Random().Next(list.Length)];
 
Sebastián said:
Hi all,
I have to randomlly pick numbers, but not any any number, it has to be from
a predefined set of posible values. Is there an "easy" way to do that????

TIA,
Sebastián
If your number selection is from a predetermined set, the selected numbers
are no loger random.
If you want to select them within a range, that is different and doable.
 
Hi all,
I have to randomlly pick numbers, but not any any number, it has to be from
a predefined set of posible values. Is there an "easy" way to do that????

TIA,
Sebastián
 
Gracias Ignacio...
So, I should create an array with the values I want the numbers from???
How do I get those values randomly?

Saludos.-
 
It is not contiguous...

Nicholas Paldino said:
In addition, if the set is contiguous, then you can just use the
overload of Next on the random class and it will return a number in that
range for you.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:[email protected]...
Hi,

What you can do is get the probables values in an array, then select a
randon from 0 .. array.length - 1

Cheers,
 
Thank you all!!!
It's been great help!


WRH said:
Hello

Something like this should work..

Random random = new Random();

int [] predef = {15,22,33,6,.....};//eg, 25 predefined mumbers

int index = random.Next(0,25);//get a random index

int num = predef[index];


Sebastián said:
Hi all,
I have to randomlly pick numbers, but not any any number, it has to be
from
a predefined set of posible values. Is there an "easy" way to do that????

TIA,
Sebastián
 
Zach said:
If your number selection is from a predetermined set, the selected numbers
are no loger random.
If you want to select them within a range, that is different and doable.

In what way is the predefined set {0, 1, 2, 3, 4} different from
selecting integers in the range [0,4], for example?

Any range of integers is a set. Picking random numbers from a set is
still picking numbers at random.
 
The set was {1,5,10,20,50,100}

Jon Skeet said:
Zach said:
If your number selection is from a predetermined set, the selected numbers
are no loger random.
If you want to select them within a range, that is different and doable.

In what way is the predefined set {0, 1, 2, 3, 4} different from
selecting integers in the range [0,4], for example?

Any range of integers is a set. Picking random numbers from a set is
still picking numbers at random.
 
Back
Top