Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

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.

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!