0

I need to generate only one number and fill this number in array with 15 elements.

My code:

        static void Main(string[] args)
        {
            int[] array = new int[15];
            Console.WriteLine("Generate array: ");
            Generator(array);
            Console.WriteLine();
            GeneratorConst(array);

            Console.ReadKey();
        }

        static void Generator(int[] array)
        {
            Random rnd = new Random();
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = rnd.Next(1, 30);
                Console.Write(array[i] + ", ");
            }
        }

        static void GeneratorConst(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i] + ", ");
            }
        }

Now I generate 15 various numbers. How can I fix it to get only one the same number in each position in 15-elements array?

  • Get `rnd.Next(1, 30);` out side of the loop ? `array[i] = rnd.Next(1, 30);` read as put a new random at index I. Imo it's simple logical mistake. this one was resolved in a way less likely to help future readers. – xdtTransform Jun 16 '20 at 07:45
  • `Enumerable.Repeat` may be worth a squiz. – mjwills Jun 16 '20 at 07:49
  • `var results = Enumerable.Repeat(rnd.Next(1, 30), 15).ToArray();` – TheGeneral Jun 16 '20 at 07:49
  • And if we are talking about array initialisation I will recommend [How to populate/instantiate a C# array with a single value?](https://stackoverflow.com/questions/1014005). – xdtTransform Jun 16 '20 at 07:50
  • But between typo and dupe of this one I will go for typo. – xdtTransform Jun 16 '20 at 07:52
  • https://stackoverflow.com/questions/1014005/how-to-populate-instantiate-a-c-sharp-array-with-a-single-value I think this one will be helpful for you. – Ultimate Jun 16 '20 at 07:52

3 Answers3

3

modify your code like so:

 static void Generator(int[] array)
    {
        Random rnd = new Random();
        var num = rnd.Next(1, 30);
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = num;
        }
    }

rnd.Next is the line where the number generation is, which means each time you call it you get a new number. by taking it out of the loop you generate a single number and then assign the number to the array every iteration

Nitzanu
  • 84
  • 8
  • You should *really* move the random object out of the method into a static field – pinkfloydx33 Jun 16 '20 at 08:56
  • why would i do such a thing? the only case this might be useful is if the developer would like the same number even on different time he calls the generator method. and even in that case i would recommend encapsulating it in a generator object instead of moving it to a static field, because IMHO static fields are just straight out a bad thing – Nitzanu Jun 17 '20 at 10:57
1

I personally like this way because it's a bit cleaner:

public static int[] Generator()
{
    var randNum = new Random().Next(1, 30);
    arr = Enumerable.Repeat(randNum, arr.Length).ToArray();
    return arr;
}
dimitar.d
  • 645
  • 1
  • 6
  • 18
  • 2
    Unless you pass `arr` as `ref` this will have no effect outside of this function? – orhtej2 Jun 16 '20 at 07:57
  • Yes, your absolutely right. I just wrapped the code in the mentioned method without giving it any thought. I will edit my suggestion right away. – dimitar.d Jun 16 '20 at 11:07
0

Something like this?

static void GeneratorConst(int[] array)
{
    Random rnd = new Random();
    var val = rnd.Next(1, 30);
    for (int i = 0; i < array.Length; i++)
    {
        array[i] = val;
        Console.Write(array[i] + ", ");
    }
}