Advertisement
jyoung12387

Alpha Numeric code generator

Mar 26th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.                    
  4. public class Program
  5. {
  6.     static Random random = new Random();
  7.     public static List<char> charList = new List<char> {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  8.                                                         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  9.                                                         'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3',
  10.                                                         '4', '5', '6', '7', '8', '9'};
  11.  
  12.     public static void Main()
  13.     {
  14.         //Display output of 10 random codes
  15.         for(int i = 0; i <10; i++)
  16.         {
  17.             DisplayCode(GetCode());
  18.         }
  19.        
  20.     }
  21.    
  22.     // Generate random 16 char string from the charList list
  23.     public static string GetCode()
  24.     {
  25.         string output = "";
  26.        
  27.         for(int i = 0; i<16; i++)
  28.         {
  29.             int charIndex = random.Next(charList.Count);
  30.             output += charList[charIndex]; 
  31.         }
  32.        
  33.         return output;
  34.     }
  35.    
  36.     // Format string with "-" every 4 chars
  37.     public static void DisplayCode(string code)
  38.     {
  39.         code = code.Insert(12,"-");
  40.         code = code.Insert(8,"-");
  41.         code = code.Insert(4,"-");
  42.        
  43.         Console.WriteLine(code);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement