Hi there, i'm a newbi at c# and i'm trying to create a game called SHUFFLE. The game is suppose to generate an array[8] of random number (without duplication) each time it is run. and then the user is suppose to sort it out in order.

I have managed to generate random numbers using Random...Next().
The only problem is that, it produces duplicates.

So could anyone please kindly help me work out how to generate an array of random numbers without duplication.

Thank

There are two dominant ways to do this, and which is better depends on what you need. More precisely, it depends wether the size of your result set is going to be smaller than the set of possible values.
For example, if you want 10 random numbers between 1 and 10 then the best approach is to produce an array of all 10 possible values and then perform a random sort. But if you want 10 random numbers between 1 and 100, then the best approach is to check each number you produce against a list of numbers already generated. If it isnt in the list then you add it.

Let us know more precisely what your requirements are and we can point you toward the better solution.

ps. Thank you for creating a new thread, it helps to keep the boards tidy and ensures you get the responses you need :)

Comments
Nice formulated answer!

There are two dominant ways to do this, and which is better depends on what you need. More precisely, it depends wether the size of your result set is going to be smaller than the set of possible values.
For example, if you want 10 random numbers between 1 and 10 then the best approach is to produce an array of all 10 possible values and then perform a random sort. But if you want 10 random numbers between 1 and 100, then the best approach is to check each number you produce against a list of numbers already generated. If it isnt in the list then you add it.

Let us know more precisely what your requirements are and we can point you toward the better solution.

ps. Thank you for creating a new thread, it helps to keep the boards tidy and ensures you get the responses you need :)

Thanks for your reply.

the numbers that needs to be generated is between 1 and 8. So doing the random sort with the array would be best :)
Will that be random every time the program is run?

That depends on how you write it. The easiest way is to use the Random class, but that is technically only pseudorandom based on a seed value. The default seed value is taken from the system clock. So as long as you dont run the application twice in the space of a second the results should be different.
As with all things random, that doesnt mean you wont get the same results from time to time...there are only a finite number of ways to rearrange eight values (40320 to be precise :)

Take a look at thte generic random sort method skanke wrote here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            RandomGenerator();
        }
        static Random rn = new Random();
        static void RandomGenerator()
        {
            List<int> mm = new List<int>();
            for (int j = 0; j < 5; j++)
            {
                int ranNums = rn.Next(5);
                if(!mm.Contains (ranNums))
                mm.Add(ranNums);
            }


            foreach(int i in mm) 
            {
                Console.WriteLine(i);
            }
        }
    }
}

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.