player2_dz

genRandomIntArr

Dec 11th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 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.          * Example Use:
  11.                 //Create randomly generated number array to feed to function
  12.                 int[] numArr = genRandomIntArr(10, -5, 5);
  13.                 //Use function to print the input array
  14.                 printIntArray(numArr);
  15.                 //Run function using input array and get result as integer
  16.                 int resultVal = returnMostFrequentInteger(numArr);
  17.          */
  18.         //Use Private Static ReadOnly Variable as it Prevents generating same number set repeatedly
  19.         //each random is initialized using the clock, if we init several new ones at same time we get same number result
  20.         //so we just init one and use rand.next to make sure we get unique number sets each time!
  21.         private static readonly Random random = new Random();
  22.         //The function itself!
  23.         static int[] genRandomIntArr(int size, int min, int max)
  24.         {
  25.             //Create num array using size input
  26.             int[] numArr = new int[size - 1];
  27.  
  28.             //For 0 to size given as input - 1 aka if size is 3 then fill array slots 0, 1, and 2
  29.             for (int j = 0; j < (size - 1); j++)
  30.             {
  31.                 //Generate number to add using min and max from input
  32.                 int randNum = random.Next(min, max);
  33.  
  34.                 //Add number to numArr
  35.                 numArr[j] = randNum;
  36.             }
  37.  
  38.             //Return result
  39.             return numArr;
  40.         }
Advertisement
Add Comment
Please, Sign In to add comment