[RESOLVED] C# Random Methods
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] C# Random Methods

  1. #1
    Join Date
    Jun 2011
    Posts
    33

    [RESOLVED] C# Random Methods

    I've been trying to write a C# program that generates random sentences. I've succeeded but I have one problem. For example, the random methods seem to only generate the same 1 random number even though I have 3. Thus, only about 9 sentences can actually be generated. After doing some testing, it appears that some sentences tend to be unique because the random methods generate numbers from a range of (1, 8) up to (1, 10).

    How do I make it so the random integers are private, rather than displayed/calculated as 1 integer?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                /* The program will close if the letter "Q" is entered at the first 'Console.ReadLine'.
                 * The loops are messy but I will fix that after I get the random number methods working.
                 */
                string x;
                Console.WriteLine("Press any key to start:");
                x = Console.ReadLine();
    
                do
                {
                    //These 3 random methods only generate 1 number.
                    int n;
                    Random rand1 = new Random();
                    n = rand1.Next(0, 8);
                    string[] names = { "Bo ", "Rad ", "Jimbo ", "Bill ", "Waldo ", "Vladislav ", "Tommy ", "Moses " };
    
                    int v;
                    Random rand2 = new Random();
                    v = rand2.Next(0, 9);
                    string[] verbs = { "killed ", "ate ", "touched ", "saw ", "talked to ", "brazed ", "threw a stone at ", "challenged ", "tickled " };
    
                    int o;
                    Random rand3 = new Random();
                    o = rand3.Next(0, 10);
                    string[] objects = { "a box ", "a wall ", "a stone ", "a machine ", "a pill ", "a fruit", "a zombie ", "a cat ", "a cornflake ", "a mugger " };
    
                    string r;
                    /*The n/o/v integers should be different but they are all the same. 'String[] names' has 9
                     * words so only about 9 types of sentences are generated, rather than the potential 720 sentences.
                    */
                    Console.WriteLine(names[n] + verbs[v] + objects[o]);
                    r = Console.ReadLine();
                }
                while (x != "Q");
            }
        }
    }
    The code acts very unpredictable so I believe it is best for anyone who wishes to figure out the problem compile and run it to observe how it works. I am using Visual C# Express 2010.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: C# Random Methods

    I think what's happening is that each call to new Random() initializes a pseudo-random number generator seeded based on the time the call occurred. Since these all happen almost simultaneously, they all get initialized with the same value and thus produce a coordinated answer.

    Usually, I declare one random number generator at the start (or even for the whole class) and call it. That is do:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                /* The program will close if the letter "Q" is entered at the first 'Console.ReadLine'.
                 * The loops are messy but I will fix that after I get the random number methods working.
                 */
                string x;
                Console.WriteLine("Press any key to start:");
                x = Console.ReadLine();
                
                Random rng = new Random(); //Declare once
    
                do
                {
                    //These 3 random methods only generate 1 number.
                    int n;
                    n = rng.Next(0, 8);
                    string[] names = { "Bo ", "Rad ", "Jimbo ", "Bill ", "Waldo ", "Vladislav ", "Tommy ", "Moses " };
    
                    int v;
                    v = rng.Next(0, 9);
                    string[] verbs = { "killed ", "ate ", "touched ", "saw ", "talked to ", "brazed ", "threw a stone at ", "challenged ", "tickled " };
    
                    int o;
                    o = rng.Next(0, 10);
                    string[] objects = { "a box ", "a wall ", "a stone ", "a machine ", "a pill ", "a fruit", "a zombie ", "a cat ", "a cornflake ", "a mugger " };
    
                    string r;
                    /*The n/o/v integers should be different but they are all the same. 'String[] names' has 9
                     * words so only about 9 types of sentences are generated, rather than the potential 720 sentences.
                    */
                    Console.WriteLine(names[n] + verbs[v] + objects[o]);
                    r = Console.ReadLine();
                }
                while (x != "Q");
            }
        }
    }
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jun 2011
    Posts
    33

    Re: C# Random Methods

    Thanks. Is there any way I can delay the call for a certain amount of milliseconds to get better randomness? The change would be unnecessary but I am curious to know how it could be done.

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: C# Random Methods

    Thread.Sleep ought to work. See: http://msdn.microsoft.com/en-us/library/d00bd51t.aspx

    You can also pass your own seed to the Random class like: new Random(seed).

    If you need cryptographic-strength randomness (you don't for this application), you can also use the RNGCryptoServiceProvider class; see http://japikse.blogspot.com/2008/10/...bers-in-c.html
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Jun 2011
    Posts
    33

    Re: C# Random Methods

    Ok, thanks.

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center