Closed Thread
Results 1 to 3 of 3

Thread: Occasional Runtime errors with PRNG?

  1. #1
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Occasional Runtime errors with PRNG?

    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
    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();
                }
    
            }
        }
    }
    Now Class1:

    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);
    
            }
        }
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    lobo521 is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    57
    Rep Power
    8

    Re: Occasional Runtime errors with PRNG?

    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;
    		}
    	}
    }

  4. #3
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Re: Occasional Runtime errors with PRNG?

    Quote Originally Posted by lobo521 View Post
    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;
    		}
    	}
    }
    Why would this stop the occasional crashes? I'm getting the seed in Class1 anyway.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. runtime error
    By gcsekhar in forum Java Help
    Replies: 2
    Last Post: 01-22-2011, 03:32 AM
  2. Runtime property
    By jaiii in forum C and C++
    Replies: 1
    Last Post: 08-19-2010, 04:43 AM
  3. Runtime error
    By lionaneesh in forum C and C++
    Replies: 8
    Last Post: 03-23-2010, 10:55 PM
  4. Runtime
    By Apprentice123 in forum C and C++
    Replies: 8
    Last Post: 11-07-2009, 06:44 AM
  5. Java without runtime?
    By ASCENT in forum Java Help
    Replies: 3
    Last Post: 12-21-2007, 07:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts