I have written a pseudo-random number generator with both my own algorithm and the pre-built algorithm. In both cases, when I enter a large number (maybe 9999999) for the length of the random string I want generated, it throws me an error (which I catch). However, on other occasions, this does not happen and the string is generated normally.
Here's my code to look at. I apologize for the comments, but I correspond with a C programmer and this makes things easier for both of us.:
BUILT-IN RANDOM CLASS:
Code:using System; using System.Text; namespace RandomGenerator { class Program { static void Main(string[] args) { //Declaring the variables string choice; int conversion; double conversion2; int sizeofstring; string charstouse = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; ///<Intro> Console.WriteLine("==RANDOM CHARACTERS GENERATOR==\nWhich would you like?\nType your selection and hit Enter.\nTo exit, press Enter without entering anything else.\n1-Number\n2-Letters\n3-Decimal\n4-Credits\n"); choice = Console.ReadLine(); //While loop for multiple calculations while (choice != "") { if (choice == "1") { //The Random class produces almost totally random (nothing can be totally random) numbers). I'm creating a new object in that class. Random randomint = new Random(); //You can't print a Random so I'm converting it to an int. The Next method gives me the NEXT value of randomint. conversion = randomint.Next(); Console.WriteLine("\nYour random number is: " + conversion); } else if (choice == "2") { Console.WriteLine("\nHow long do you want your string of letters to be?\nType the length and then press Enter.\n"); ///<Exception Handling> This is telling the program try the code in the try block. If an error occurs, go the catch block. try { //sizeofstring is converting what the user typed to an integer. sizeofstring = Convert.ToInt32(Console.ReadLine()); //Calling a method and passing these parameters RandomString(sizeofstring, charstouse); } //If you typed in a letter or something, it sends you here. catch { Console.WriteLine("\nYou thought you'd be funny and try to break the program, eh? No funny business is allowed here.\nA person like you doesn't deserve to use this program.\nPress Enter to exit."); Console.ReadLine(); //Environment.Exit tells the CONSOLE Application to close. Environment.Exit(1); } } else if (choice == "3") { //Same process as "int" except we're replacing the ints with doubles. Random randomdouble = new Random(); conversion2 = randomdouble.NextDouble(); Console.WriteLine("\nYour random decimal is: " + conversion2); } else if (choice == "4") { //Prints credits Console.WriteLine("Created by Smashbrosboy\nCopyright 2009 Smashbrosboy\nContact me: sbboyjunkmail@gmail.com\n"); } else if (choice == "42") { Console.WriteLine("\nYeah, that's the answer to the ultimate question, but what IS the ultimate question?\nPress Enter to continue."); Console.ReadLine(); } //The || pipes act like the word or. else if (choice == "No" || choice == "no") { Console.WriteLine("\nCome on man, I know you're bored but still. At least give me a chance to show off if nothing else.\nPress Enter to continue."); Console.ReadLine(); } else { Console.WriteLine("\nYoda: Amazingly funny are you, yet not laughing am I /sarcasm\nPress Enter to continue."); Console.ReadLine(); } Console.WriteLine ("\nSelect again or press Enter without typing anything to exit.\n1-Number\n2-Letters\n3-Decimal\n4-Credits\n"); //Resetting the value of choice to prevent infinite looping. choice = Console.ReadLine(); } } //This method returns a string value. //This method has two parameters that were passed to it. One int, sizeofstring that was passed and will be now called size in the method //and one string which was passed to it, charstouse, which is now called legalChars in the method. public static void RandomString(int size, string legalChars) { //Declaring a new random object Random z = new Random(); //String builder is self-explanatory StringBuilder sb = new StringBuilder(); //A for loop is used here to write as many characters as sizeofstring (now called size) dictates. for (int i = 0; i < size; i++) { //This is the most complex line of the code: //Within the string legalChars, we are calling upon a certain letter. //This letter is determined by the random object z which only generates numbers. //z is limited to only generate numbers that are smaller than or equal to the length of the string. Thus, when it generates a number, it will correspond to a letter. //The stringbuilder sb is appending the letter which corresponds to this number within legalChars. sb.Append(legalChars[z.Next(legalChars.Length)]); } Console.WriteLine("\nYour random string is:\n" + sb); } } }
MY OWN ALGORITHM:
First, Program.cs
Now Class1:Code:using System; using System.Text; namespace RandomGenerator { class Program { static void Main(string[] args) { //Declaring the variables string choice; double conversion2; int sizeofstring; ///<Intro> Console.WriteLine("==RANDOM CHARACTERS GENERATOR==\nWhich would you like?\nType your selection and hit Enter.\nTo exit, press Enter without entering anything else.\n1-Number\n2-Letters\n3-Decimal\n4-Credits\n"); choice = Console.ReadLine(); //While loop for multiple calculations while (choice != "") { if (choice == "1") { /*//The Random class produces almost totally random (nothing can be totally random) numbers). I'm creating a new object in that class. Random randomint = new Random(); //You can't print a Random so I'm converting it to an int. The Next method gives me the NEXT value of randomint. conversion = randomint.Next(); Console.WriteLine("\nYour random number is: " + conversion);*/ Class1 obj = new Class1(); } else if (choice == "2") { Console.WriteLine("\nHow long do you want your string of letters to be?\nType the length and then press Enter.\n"); ///<Exception Handling> This is telling the program try the code in the try block. If an error occurs, go the catch block. try { //sizeofstring is converting what the user typed to an integer. sizeofstring = Convert.ToInt32(Console.ReadLine()); DateTime var = DateTime.Now; int seed = var.Millisecond; //Calling a method and passing these parameters //RandomString(sizeofstring, charstouse); Class1 obj = new Class1(sizeofstring, seed); } //If you typed in a letter or something, it sends you here. catch { Console.WriteLine("\nYou thought you'd be funny and try to break the program, eh? No funny business is allowed here.\nA person like you doesn't deserve to use this program.\nPress Enter to exit."); Console.ReadLine(); //Environment.Exit tells the CONSOLE Application to close. Environment.Exit(1); } } else if (choice == "3") { //Same process as int except we're replacing int with double. DateTime v = DateTime.Now; double s = v.Millisecond; Class1 c1 = new Class1(s); } else if (choice == "4") { //Prints credits Console.WriteLine("Created by Smashbrosboy\nCopyright 2009 Smashbrosboy\nContact me: sbboyjunkmail@gmail.com\n"); } //I'm not explaining the remaining else ifs and elses in detail. else if (choice == "42") { Console.WriteLine("\nYeah, that's the answer to the ultimate question, but what IS the ultimate question?\nPress Enter to continue."); Console.ReadLine(); } //The || pipes act like the word or. else if (choice == "No" || choice == "no") { Console.WriteLine("\nCome on man, I know you're bored but still. At least give me a chance to show off if nothing else.\nPress Enter to continue."); Console.ReadLine(); } else { Console.WriteLine("\nYoda: Amazingly funny are you, yet not laughing am I /sarcasm\nPress Enter to continue."); Console.ReadLine(); } Console.WriteLine ("\nSelect again or press Enter without typing anything to exit.\n1-Number\n2-Letters\n3-Decimal\n4-Credits\n"); //Resetting the value of choice to prevent infinite looping. choice = Console.ReadLine(); } } } }
Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace RandomGenerator { class Class1 { string legalchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; public Class1(int size, int seed) { StringAlgorithm(size,seed); } public Class1(double seed) { double result; Console.WriteLine("Would you like any random decimal or only one that is less than 1?\n1-Any\n2-Less Than 1"); string read = Console.ReadLine(); if (read == "1") { if ((seed % 2) == 0) { result = seed * 12.5877 + 71 % 100 * 65 / 89 % 70; Console.WriteLine(result); } else { result = seed * 0.245602 + 84 % 100 * 4 / 98 % 87; Console.WriteLine(result); } } else if (read == "2") { if ((seed % 2) == 0) { result = seed * 12.5877 + 71 % 100 * 65 / 89 % 70; while (result < 0) { result += 0.5; } while (result > 1) { result -= 0.5; } Console.WriteLine(result); } else { result = seed * 0.245602 + 84 % 100 * 4 / 98 % 87; while (result < 0) { result += 0.5; } while (result > 1) { result -= 0.5; } Console.WriteLine(result); } } else { Console.WriteLine("Invalid Choice. Stop messing with the program. Press Enter to Exit."); Console.ReadLine(); Environment.Exit(1); } } public Class1() { DateTime var = DateTime.Now; int seed = var.Millisecond; Algorithm(seed); } void Algorithm(int seed) { int result; if ((seed % 2) == 0) { result = seed * 283 + 71 % 100; } else { result = seed * 1236599832 + 71 % 100; } Console.WriteLine(result); } void StringAlgorithm(int size, int seed) { StringBuilder sb = new StringBuilder(); while (seed == 0) { DateTime var = DateTime.Now; seed = var.Millisecond; } int result = seed * 2 + 7 % 100+97/5*100%40; for (int i = 0; i < size; i++) { result = seed * 2 + 7 % 100+97/5*100%40; try { while (result > 61) { result -= 50; result += 1; } while (result < 0) { result += 50; result -= 1; } } catch { result = 23; } sb.Append(legalchars[result]); seed = result; } Console.WriteLine("\nYour random string is:\n" + sb); } } }
I tried like that and it's not crashing ;/
Code:using System; namespace RandomGenerator { class Program { static void Main(string[] args) { //While loop for multiple calculations while (true) { new Class1(9999999, GetSeed()); } } private static int GetSeed() { return DateTime.Now.Millisecond; } } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks