Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to Generate Many Random Various Numbers?

4.94/5 (26 votes)
28 Jan 2011CPOL 74.6K  
How to generate many random various numbers?
  1. We need an array of random various numbers between x[0] and x[n] like shuffling the cards.

    For example: 8, 10, 5, 4, 7, 6, 3, 1, 9, 2 (between 1 and 10)

    The following code is a method (with an overload) and generates an array of random various integer numbers:

    C#
    //C#
    public static int[] RandomNumbers(int min, int max)
    {
        return RandomNumbers(min, max, 2);
    }
    public static int[] RandomNumbers(int min, int max, int derangement)
    {
        if (min > max)
        {
            throw new Exception("The first parameter must be less (or equal) than the second.");
        }
        Random random = new Random();
        int count = max - min; ;
        int[] tempList = new int[count + 1];
        int counter = 0;
        for (int i = min; i <= max; i++)
        {
            tempList[counter] = i;
            counter++;
        }
    
        for (int i = 0; i < derangement; i++)
            for (int j = 0; j < count; j++)
            {
                int k = random.Next(0, count + 1);
                int l = random.Next(0, count + 1);
                if (k != l)
                {
    		    //Swap TempList[k] with TempList[l]
                    tempList[k] += tempList[l];
                    tempList[l] = tempList[k] - tempList[l];
                    tempList[k] = tempList[k] - tempList[l];
                }
            }
        return tempList;
    }

    Note: derangement is an integer variable to define the chance of the numbers derangement, but there is an inverse relationship between the performance and the value of the variable. The overload default is 2.

    Now, it's ready to use the method:

    C#
    //C#
    int[] m = RandomNumbers(1, 56, 5);
    foreach (int i in m)
    {
        Console.Write("{0}, ",i);      
    }
    Console.ReadKey();
  2. We need to generate a random number which has not been generated since runtime.

    There has been a generated random number previously, but we need to have a different one:

    C#
    //C#
    public static int RandomNumber(ref List<int> numbers, Random random)
    {
        int count = numbers.Count;            
        int randomIndex = random.Next(0, count);
        int returnedNumber = numbers[randomIndex];
        numbers.RemoveAt(randomIndex);
        return returnedNumber;
    }

    It's required for using the method, to have a random variable and a list of numbers, for example:

    C#
    //C#
    Random random = new Random();
    List<int> numbers = new List<int>();
    for (int i = 0; i <= 1000; i++)
    {
        numbers.Add(i);
    }
    
    //Ok, For each using the method, there is a new and different random number:
    
    Console.WriteLine(RandomNumber(ref numbers, random));
    Console.WriteLine(RandomNumber(ref numbers, random));
    Console.WriteLine(RandomNumber(ref numbers, random));
    Console.WriteLine(RandomNumber(ref numbers, random));
    Console.WriteLine(RandomNumber(ref numbers, random));
    Console.ReadKey();

The second approach has better performance.

Good luck!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)