Advertisement
player2_dz

genRandomIntArr

Dec 11th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1.         /*
  2.          * Function Name:   genRandomIntArr
  3.          * Description:     Generates an array of integers based on the inputs given
  4.          * Inputs:
  5.          *      Int size - The amount of numbers that should be in the integer array
  6.          *      Int min - The minimum value each generated number can be
  7.          *      Int max - The maximum value each generated number can be
  8.          * Function Outputs:
  9.          *      Int[] numArr - An array of integers
  10.          */
  11.         //Use Private Static ReadOnly Variable as it Prevents generating same number set repeatedly
  12.         //each random is initialized using the clock, if we init several new ones at same time we get same number result
  13.         //so we just init one and use rand.next to make sure we get unique number sets each time!
  14.         private static readonly Random random = new Random();
  15.         //The function itself!
  16.         static int[] genRandomIntArr(int size, int min, int max)
  17.         {
  18.             //Create num array using size input
  19.             int[] numArr = new int[size - 1];
  20.  
  21.             //For 0 to size given as input - 1 aka if size is 3 then fill array slots 0, 1, and 2
  22.             for (int j = 0; j < (size - 1); j++)
  23.             {
  24.                 //Generate number to add using min and max from input
  25.                 int randNum = random.Next(min, max);
  26.  
  27.                 //Add number to numArr
  28.                 numArr[j] = randNum;
  29.             }
  30.  
  31.             //Return result
  32.             return numArr;
  33.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement