How can I randomly pick either 5 or 8??

  • Thread starter Thread starter Paul Tomlinson
  • Start date Start date
P

Paul Tomlinson

How can I randomly pick either the value integer 5 or integer 8??

I don't want a value between 5 and 8 but either the values 5 or 8 randomly.
Any ideas on how can I do this please??

MA
 
Hello

You can try something like this:

double rnd = System.Randow.NextDouble();
int fiveOrEight = (rnd < 0.5) ? 5 : 8;

Aleksandar
 
try this:

Create a System.Random type that generates a value between 1 and 12. ( I
chose 1 - 12 so that the value '8' has the same distance to the "upper bound"
as '5' does to the "lower bound")

Subtract the random value from 5, then from 8. Which ever operation
produces the smallest absolute value, then use the value (5 or 8)that
produced this result. For instance, if the random value is 6, then then the
results will be -1, and 2. (5 - 6) results in the smallest absolute value,
therefore 5 would be selected based on this logic.
 
Thanks both. I forgot to mention that 5 and 8 are variable. I'll work with
the 0.5 double I think :->
 
Paul Tomlinson said:
How can I randomly pick either the value integer 5 or integer 8??

I don't want a value between 5 and 8 but either the values 5 or 8
randomly.
Any ideas on how can I do this please??

MA

Pick a number from 0 to 1 (integer), if 0 then choose 5, otherwise choose 8
:)

Mythran
 
Back
Top