How to create an array of n random integers with a function and list on declaration in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = CreateArray(13, 0, 100);

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

        private static int[] CreateArray(int total_elements, int lower, int upper)
        {
            Random rnd = new Random();
            List<int> list = new List<int>();

            for (int i = 1; i <= total_elements; i++)
                list.Add(rnd.Next(lower, upper + 1));

            return list.ToArray();
        }
    }
}


/*
run:
 
26  24  14  75  8  15  76  10  76  49  83  82  98 

*/

 



answered Apr 16, 2016 by avibootz
...