简体   繁体   中英

fill array with random numbers

I have a little problem. I want fill my array with 100 random numbers. The problem is i have a lot of the same numbers 2/4 of my array is 0 then 1/4 89 andso how is this possible ???

thanks for helping

public void arrayreverse()
{
    int[] arr = new int[100];
    Random r = new Random();

    for (int i = 0; i < 100; i++)
    {
        int rand = r.Next(0, 100);
        arr[i] = rand;
        Array.Sort(arr);
        Array.Reverse(arr);
        Console.WriteLine(arr[i]);
    }
    Console.ReadLine();
}

Since you sort and reverse the array inside the loop you basically force the biggest number to be last.

For example in 5 sized array:

i = 0
arr[i] = rand:
arr -> 5 0 0 0 0

sort and reverse
arr -> 5 0 0 0 0
output = 5

i = 1
arr[i] = rand:
arr -> 5 8 0 0 0

sort and reverse
arr -> 8 5 0 0 0
output = 5

i = 2
arr[i] = rand:
arr -> 8 5 7 0 0

sort and reverse
arr -> 8 7 5 0 0
output = 5

i = 3
arr[i] = rand:
arr -> 8 5 7 2 0

sort and reverse
arr -> 8 7 5 2 0
output = 2

i = 4
arr[i] = rand:
arr -> 8 7 5 2 0

sort and reverse
arr -> 8 7 5 2 0
output = 2

As you can see, your output will always be the smallest number and as you go on, less and less number generated by the Random.Next can be smaller than the current smallest so the current smallest will most likely appear again as the output.

If you want to make a list of 100 numbers and print them you can use this Linq :

(new int[100])
    .Select(x => r.Next(100))
    .ToList()
    .ForEach(x => Console.WriteLine(x));

If you want the numbers to be distinct (all the numbers between 0 and 99 but you want them to appear only once) you can use the following code:

private static Random r = new Random();

public static int[] GetRandomArray(int size)
{
    SortedDictionary<double, int> sortedSet = new SortedDictionary<double, int>();
    for (int index = 0; index < size; index++)
    {               
        sortedSet.Add(r.NextDouble(), index);
    }
    return sortedSet.Select(x => x.Value).ToArray();
}

LINQ is easier to get right:

var randomNumbers = 
      Enumerable.Range(0, 100) // create sequence of 100 elements
      .Select(_ =>r.Next(0,100)) // for each element select random value
      .ToArray(); // convert to array.

As for your current sample - move sorting out of the for loop to fix bug.

"Random" doesn't imply unique. You can still achieve the expected behavior by using a set:

    var takenNumbers = new SortedSet<int>();

    while (takenNumbers.Count != 100)
    {
        takenNumbers.Add(r.Next(0, 100));
    }

    var array = takenNumbers.ToArray();

It seams, however, that you want to build a unique sequence , not a set.

Here is code building a sequence:

    var takenNumbers = new HashSet<int>();

    var array = int[100];
    while (takenNumbers.Count != 100)
    {
        int rand = r.Next(0, 100);
        if (takenNumbers.Add(rand))
        {
           array[takenNumbers.Count - 1] = rand; 
        }
    }

    foreach(var number in arr)
    {
       Console.WriteLine(number);
    }
public void Arrayreverse()
{
    int[] arr = new int[100];
    Random r = new Random();

    for (int i = 0; i < 100; i++)
    {
        int rand = r.Next(0, 100);
        arr[i] = rand;
    }

    Array.Sort(arr);
    Array.Reverse(arr);

    for (int i = 0; i < 100; i++)
    {
        Console.WriteLine(arr[i]);
    }
    Console.ReadKey();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM