CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2011
    Posts
    33

    System.Random is a type but is used like a variable

    I keep getting this error message with my program and I don't know how to fix it. Basically, I want the program to generate a set of random numbers using the specified ranges. However, I want 23 different numbers and instead of writing 23 lines of code for every individual random method, I want the two random methods I have to generate a different number every time it is called.

    The two error messages are "System.Random is a type but is used like a variable".

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace RandomWord
    {
        class WordProcess
        {
            static void Main(string[] args)
            {
                /* The program will close if the sequence "f*" is entered at the first 'Console.ReadLine'.
                 */
                string x;
                Console.WriteLine("Press any key to start:");
                x = Console.ReadLine();
    
                do
                {
                    Random rng = Random((int)DateTime.Now.Ticks & 0x0000FFFF);
                    Thread.Sleep(15);
                    Random rng2 = Random((int)DateTime.Now.Ticks & 0x0000FFFF);
                    Thread.Sleep(15);
    
                    int con = rng.Next(0, 21);
                    int vo = rng2.Next(0, 5);
    
                    string[] conlist = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v" };
                    string[] volist = { "a", "e", "i", "o", "u" };
    
                    Thread.Sleep(10);
                    Console.WriteLine(conlist[con] + volist[vo] + conlist[con] + volist[vo] + conlist[con] + volist[vo]);
                    Console.ReadLine();
                }
                while (x != "f*");
            }
        }
    }

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

    Re: System.Random is a type but is used like a variable

    Code:
    Random rng = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
    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
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: System.Random is a type but is used like a variable

    Ok, actually the code is a lot more structurally broken than just the syntax error. However! That's fine, you're a student. Below is an example and some tips (offered in the spirit of helping you learn).

    First, what's with the two Random number generators? There are essentially no situations where you need two random number generators. Random will continue giving you new random numbers every time you call .Next(...).

    If for some reason you HAD to have to RNGs (you don't!), you certainly shouldn't initialize them by waiting with Thread.Sleep(). Those two calls will cause your program to wait 30 msec every time you go through the loop.

    Declare a random number generator (and the arrays that never change) ONCE outside the loop:

    Code:
    string x;
    Console.WriteLine("Press any key to start:");
    x = Console.ReadLine();
    
    //Move these out of the loop
    string[] conlist = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v" };
    string[] volist = { "a", "e", "i", "o", "u" };
    
    //Move out of the loop and declare exactly once
    Random rng = new Random(); //A parameterless constructor defaults to using DateTime.Now.Ticks; 
                               // you do not need to explicitly specify
    
    do
    {
        int con = rng.Next(0, 21);
        int vo = rng.Next(0, 5);    //Not rng2, just rng    
    
        Thread.Sleep(10);    //This line of code serves no functional purpose; delete it!
        Console.WriteLine(conlist[con] + volist[vo] + conlist[con] + volist[vo] + conlist[con] + volist[vo]);
        Console.ReadLine();
    }
    while (x != "f*");
    Last edited by BioPhysEngr; May 3rd, 2012 at 09:14 PM. Reason: improve tone a little to faciliate learning; fixed a syntax typo
    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.

  4. #4
    Join Date
    Feb 2008
    Posts
    108

    Re: System.Random is a type but is used like a variable

    int con = rng.Next(0, 21);

    The conlist is only 17 elements, but con could be as large as 21, so eventually it may crash by being out of bounds.
    Developing using:
    .NET3.5 / VS 2010

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: System.Random is a type but is used like a variable

    Good catch, Jim!

    Code review is the best debugging technique!
    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.

  6. #6
    Join Date
    Feb 2008
    Posts
    108

    Re: System.Random is a type but is used like a variable

    Thanks, BPE.
    x = Console.ReadLine();
    This line also needs to be within the do-while loop, otherwise x never gets updated.
    Developing using:
    .NET3.5 / VS 2010

  7. #7
    Join Date
    Jun 2011
    Posts
    33

    Re: System.Random is a type but is used like a variable

    I updated the code but the variables are still not uniquely random. For example, the code output is this:

    Press any key to start:
    kukuku
    rarara
    vovovo
    bibibi

    However, I want the letters to be different every time they are printed, for example:

    Press any key to start:
    kudibu
    birupa
    voraku
    bikuvo

    That way, the words will be truly "random", rather than repeating. I could simply add 4 more rng.Next lines of code, but I plan to turn the program into a full sentence generator of 24 random words so creating 24 .Next lines of code for every random letter seems impractical.
    Last edited by Krunchyman; May 4th, 2012 at 05:41 PM.

  8. #8
    Join Date
    Feb 2008
    Posts
    108

    Re: System.Random is a type but is used like a variable

    The code is doing what you told it to do in this statement:
    Console.WriteLine(conlist[con] + volist[vo] + conlist[con] + volist[vo] + conlist[con] + volist[vo]);
    since you are only using one index variable for constants & one for vowels. If you generated indices for 3 vowels & 3 constants, then executed this statement:
    Console.WriteLine(conlist[con1] + volist[vo1] + conlist[con2] + volist[vo2] + conlist[con3] + volist[vo3]);
    I believe this is what you are trying to do.
    Developing using:
    .NET3.5 / VS 2010

  9. #9
    Join Date
    Jun 2011
    Posts
    33

    Re: System.Random is a type but is used like a variable

    Quote Originally Posted by Jim_Auricman View Post
    The code is doing what you told it to do in this statement:
    Console.WriteLine(conlist[con] + volist[vo] + conlist[con] + volist[vo] + conlist[con] + volist[vo]);
    since you are only using one index variable for constants & one for vowels. If you generated indices for 3 vowels & 3 constants, then executed this statement:
    Console.WriteLine(conlist[con1] + volist[vo1] + conlist[con2] + volist[vo2] + conlist[con3] + volist[vo3]);
    I believe this is what you are trying to do.
    Yes, but I don't want to have to specify integers such as conlist[con1] all the way through conlist[17]. If I did it that way, I'd have to specify each individual integer as well as the rng.Next statements, which would add up to 24 integers I'd have to manually write out. I think it's a bad programming habit.

    I'm basically trying to figure out how to simplify the code so I don't have to enter in tons of numbers manually.
    Last edited by Krunchyman; May 4th, 2012 at 08:24 PM. Reason: Describing a statement more properly

  10. #10
    Join Date
    Feb 2008
    Posts
    108

    Re: System.Random is a type but is used like a variable

    You just need 6 variables:
    int con1 = rng.Next(0, 17);
    int vo1 = rng.Next(0, 5);
    int con2 = rng.Next(0, 17);
    int vo2 = rng.Next(0, 5);
    int con3 = rng.Next(0, 17);
    int vo3 = rng.Next(0, 5);
    This is put into the loop right before you do the Console.WriteLine statement.
    I actually coded it and it looks like it is performing like you intended.
    Developing using:
    .NET3.5 / VS 2010

  11. #11
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: System.Random is a type but is used like a variable

    Your problem is one of data representation and you are right to want to avoid writing it out manually. I propose a solution that looks like this:

    Code:
    //Give a pattern of the form "cvvvcv" to mean ("consonant-vowel-vowel-vowel-consonant-vowel)
    public static string getRandomString(string pattern, string[] conList, string[] vowelList, Random rng)
    {
        System.Text.StringBuilder result = new StringBuilder(pattern.Length);
        for(int i = 0; i < pattern.Length; i++)
        {
            switch(pattern[i])
            {
                case 'c':
                    result.Append(conList[rng.Next(0, conList.Length)]);
                    break;
                case 'v':
                    result.Append(vowelList[rng.Next(0, vowelList.Length)]);
                    break;
                default:
                    throw new ArgumentException("Invalid pattern");
            }
        }
        
        return result.ToString();
    }
    Then on each loop just call:

    Code:
    getRandomString("cvcvcv", conList, vowelList, rng);
    Make sense?
    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.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured