Public Group

Random number question

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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]

Share on other sites
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?

Share on other sites
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.

Share on other sites
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?

Share on other sites
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.

Share on other sites

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.

Share on other sites

[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.

Share on other sites
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.

Share on other sites
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).

Share on other sites

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.

1. 1
2. 2
Rutin
20
3. 3
A4L
15
4. 4
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633738
• Total Posts
3013617
×