Advertisement
jyoung12387

Fill array with random values

Mar 15th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ArrayofValues
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             #region BytesExample
  14.             byte[] bytes1 = new byte[100];
  15.             Random rnd1 = new Random();
  16.  
  17.             rnd1.NextBytes(bytes1);
  18.  
  19.             Console.WriteLine("First Series:\n");
  20.             for (int ctr = bytes1.GetLowerBound(0);
  21.                  ctr <= bytes1.GetUpperBound(0);
  22.                  ctr++)
  23.             {
  24.                 Console.Write("{0,5}", bytes1[ctr]);
  25.                 if ((ctr + 1) % 10 == 0)
  26.                 {
  27.                     Console.WriteLine();
  28.                 }
  29.             }
  30.             #endregion
  31.  
  32.             LineDivider();
  33.  
  34.             #region Integer Example
  35.             int[] numbers = new int[100];
  36.             Random random = new Random();
  37.  
  38.             int min = 1;
  39.             int max = 1000;
  40.  
  41.             Console.WriteLine("Example using integers:\n");
  42.             for (int i = 0; i < numbers.Length; i++)
  43.             {
  44.                 numbers[i] = random.Next(min, max - 1);
  45.                 Console.Write($"{numbers[i],5}");
  46.                 if ((i + 1) % 10 == 0)
  47.                 {
  48.                     Console.WriteLine();
  49.                 }
  50.             }
  51.             #endregion
  52.  
  53.             LineDivider();
  54.  
  55.             #region Example Using Chars
  56.  
  57.             //Instantiate new array to store data, and new Random class to generate random values
  58.             char[] letters = new char[100];
  59.             Random rand = new Random();
  60.            
  61.             //Define min and max values
  62.             char minChar = 'A';
  63.             char maxChar = 'Z';
  64.  
  65.             //Write Output to Console
  66.             Console.WriteLine("Example using characters:\n");
  67.             for (int i = 0; i < letters.Length; i++)
  68.             {
  69.                 letters[i] = (char)rand.Next(minChar, maxChar - 1);
  70.                 Console.Write($"{letters[i],5}");
  71.                
  72.                 ///<Summary>
  73.                 /// Each time though it checks to see if i is a factor of 10. If no it continues through loop writing to the current                line.
  74.                 /// If yes, it writes a new line to write on.
  75.                 /// </Summary>
  76.                 if ((i + 1) % 10 == 0)      
  77.                 {
  78.                     Console.WriteLine();
  79.                 }
  80.             }
  81.  
  82.             Console.ReadLine();
  83.             #endregion
  84.         }
  85.         public static void LineDivider()
  86.             {
  87.             Console.WriteLine("\n--------------------------------------------------\n");
  88.             }
  89.     }
  90.  
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement