1

I need to assign values to an IntArray with 1000 elements randomly. But the elements cannot be repeated. I used this code...

public int[] Numbers()
{
    Random random = new Random();
    int check;

    for (int i = 0; i < numbers.Length; i++)
    {
        check = random.Next(0, 9999);

        while (!numbers.Contains(check))
        {
            numbers[i] = check;
        }
    }

    return numbers;
}

But then, an amount of numbers get the default values (0). What am I doing wrong ?

1
  • Oh sorry, forgot to translate that part xD Commented Jan 28, 2013 at 15:42

5 Answers 5

4

Your logic is slightly off - if the Contains() fails, you need to still assign that index:

for (int i = 0; i < numbers.Length; i++)
{
    check = random.Next(0, 9999);

    while (numbers.Contains(check))
    {
        // The number existed, so recompute...
        check = random.Next(0, 9999);
    }

    numbers[i] = check;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You're skipping over entries if they already exist, which is why they are 0. Change your while loop:

for (int i = 0; i < numbers.Length; i++)
{
    check = random.Next(0, 9999)
    while (numbers.Contains(check))
    {
        check = random.Next(0, 9999)
    }

    numbers[i] = check;
}

Although a more performant (trades memory versus loops over numbers) means of generating N distinct random numbers:

int ii = 0;
var numbers = new int[N];
var used = new HashSet<int>(); // much faster on lookups than Array.Contains
while (used.Count < N)
{
    var check = random.Next();
    if (used.Add(check)) numbers[ii++] = check;
    // alternatively: if (used.Add(numbers[ii] = check)) ii++;
}

return numbers;

Comments

0

You could do something like this, although it is not necessarily the most performant:

var rnd = new Random();
var result = Enumerable.Range(0, 10000).OrderBy(i => rnd.Next()).Take(1000).ToArray();

Comments

-1

When the check fails you skip a number. Change the loop so that if the check fails you generate a new number before the loop continues

Comments

-1

Another way to look at this will be to shuffle an order array of 1000 items You can use something like this:

        public T[] Shuffle<T>(T[] array)
        {
                var random = _random;
                for (int i = array.Length; i > 1; i--)
                {
                        // Pick random element to swap.
                        int j = random.Next(i); // 0 <= j <= i-1
                        // Swap.
                        T tmp = array[j];
                        array[j] = array[i - 1];
                        array[i - 1] = tmp;
                }
                return array;
        }

and then use it like that

                int[] values = new int[1000]
                for (int i=0; i<999; i++)
                   values[i] = i;

                values = Shuffle<int>(values);
                foreach (int item in values)
                {
                        Response.Write(item);
                }
                Response.Write("</br>");
                values = Shuffle<int>(values);
                foreach (int item in values)
                {
                        Response.Write(item); //this will generate a unique random from 0-999
                }

code example taken from here

11 Comments

Shuffling an array and generating 1000 unique random numbers are two mutually exclusive goals.
OP wants 1000 distinct integers between 0 and 9999. How do you fill the array before shuffling it to achieve this?
@sixlettervariables: If you have a sorted array of 1000 distinct integers, you can shuffle it to get a random permutation.
just fill an array with a simple loop from 0-9999 and the shuffle it.
@Mortalus: 0-9999 is 10,000 numbers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.