Random number question

Started by
14 comments, last by phil67rpg 12 years, 3 months ago
well I am able to print out 9 random numbers using c#.however I want to print out 9 random numbers but do not want the numbers to repeat.
here is the code I am using.
[source lang = "c#"]
static void Main(string[] args)
{
Random num = new Random();
int count = 1;
while (count <= 9)
{
int rand = num.Next(1, 10);
Console.Write(rand);
Console.WriteLine("");
count++;
}
}
[/source]
Advertisement
If you don't want the numbers to repeat, then you need to somehow keep track of what numbers have already been selected. Or, since you're selecting exactly 9 numbers from exactly 9 possible numbers, why not select-and-remove from an array?
Looks like you're trying to shuffle a set of numbers. The Fisher-Yates Shuffle algorithm is pretty decent for shuffling things and it's pretty simple. You're picking 9 numbers from a list of ten (numbers 1-10), so you can just drop the last number in the shuffle and have your 9 random numbers.

i.e. make a list of numbers 1-10, shuffle it, and the first 9 numbers (because you're using 9 numbers and not all 10) in the shuffled array are your 9 random numbers.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Pretty sure the C# Random class seeds itself with the current time, so you shouldn't be getting repeating numbers. What numbers are you getting?
I think he means not getting results such as 4-7-1-4-5-4 with multiple 4's in the example for instance. The best way to do this is just to use a shuffle (Fisher-Yates as mentioned is very good). Basically:

- fill in a 9-integer array E as {1, 2, 3, 4, 5, 6, 7, 8, 9}
- for each element E[N] in the array:
--- pick a random number R between 1 and 9 (inclusive)
--- swap E[N] and E[R]

Then you just read the array elements one by one, you should be getting something like E = {2, 6, 3, 9, 7, 1, 5, 8, 4}. They can never repeat since the original array contains distinct numbers and you only perform swaps.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


Pretty sure the C# Random class seeds itself with the current time, so you shouldn't be getting repeating numbers.


The C# Random class is also... not very random. For certain constraints it performs very poorly. For the OP, others have provided good answers.

[quote name='Narf the Mouse' timestamp='1325492637' post='4898888']
Pretty sure the C# Random class seeds itself with the current time, so you shouldn't be getting repeating numbers.


The C# Random class is also... not very random. For certain constraints it performs very poorly. For the OP, others have provided good answers.
[/quote]
While true, he's only generating 9 numbers - He shouldn't be seeing a lack of randomness with that small a sample size.
Actually, you'd be surprised how few random numbers you need to generate from a fixed range before you have a collision. Even with a proper source of randomness such as radioactive decay.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
Actually, you'd be surprised how few random numbers you need to generate from a fixed range before you have a collision. Even with a proper source of randomness such as radioactive decay.[/quote]
For those wondering, Birthday Paradox. So you need to devise some kind of algorithm to ensure numbers do not repeat (collisions do not happen). There are lots of ways to do this but the most used and the simplest in practice is the Fisher-Yates shuffle (of course there are lots of variations depending what you need it for, for instance if your number range is subject to change often then it might not be the best option).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


Actually, you'd be surprised how few random numbers you need to generate from a fixed range before you have a collision. Even with a proper source of randomness such as radioactive decay.

Good point.

This topic is closed to new replies.

Advertisement