Results 1 to 10 of 10

Thread: [RESOLVED] Console Random number to variable

  1. #1

    Thread Starter
    Addicted Member Cyberflyz's Avatar
    Join Date
    Apr 2007
    Location
    Utah
    Posts
    165

    Resolved [RESOLVED] Console Random number to variable

    Hello,

    I am making a simple console game. If you have ever heard of the board game code breaker you will understand better.

    In my version of the game you will have to figure out a four number combination. The only numbers it can be is 1, 2, 3, or 4.

    I need to generate four random numbers and put each on into its own variable. This allows me to call back each number.

    How do I do this? I have been doing some research on random numbers and I use this code to generate four random numbers

    Code:
    Dim rnd As New Random()
    
          Console.WriteLine("20 random integers from -100 to 100:")
          For ctr As Integer = 1 To 20
             Console.Write("{0,6}", rnd.Next(-100, 101))
             If ctr Mod 5 = 0 Then Console.WriteLine()
          Next
          Console.WriteLine()
    But I can't figure out to put each number into a variable so I can call the same numbers back.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Console Random number to variable

    It's the rnd.Next method that returns the random number. Instead of passing it straight to Console.Write, assign it to a variable. Done.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Cyberflyz's Avatar
    Join Date
    Apr 2007
    Location
    Utah
    Posts
    165

    Re: Console Random number to variable

    im not entirely sure who to send it to a variable... here is the code as is now

    Code:
    Sub Main()
    
            Dim rnd As New Random()
    
    
            Console.WriteLine("4 random numbers which are either a 1, 2, 3, or 4:")
    
            Console.WriteLine()
    
            For ctr As Integer = 1 To 2
    
                Console.Write("{0,6}", rnd.Next(1, 9))
    
                If ctr Mod 5 = 0 Then Console.WriteLine()
    
            Next
    
            Console.WriteLine()
    
        End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Console Random number to variable

    You're saying that you don't know how to call a function and assign the result to a variable? I think you do because I think you've probably done it many times before. You can either do it with discrete variables:
    vb.net Code:
    1. Dim var1 As Integer
    2. Dim var2 As Integer
    3.  
    4. var1 = myRandom.Next(min, max)
    5. var2 = myRandom.Next(min, max)
    or you can use an array:
    vb.net Code:
    1. Dim arr(count) As Integer
    2.  
    3. For i = 0 To count - 1
    4.     arr(i) = myRandom.Next(min, max)
    5. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member Cyberflyz's Avatar
    Join Date
    Apr 2007
    Location
    Utah
    Posts
    165

    Re: Console Random number to variable

    Im way sorry...I feel dumb.

    I don't entirely know how to use the myRandom part...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Console Random number to variable

    It's your Random.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member Cyberflyz's Avatar
    Join Date
    Apr 2007
    Location
    Utah
    Posts
    165

    Re: Console Random number to variable

    I got it to work. Dang, i'm way sorry...im so slow

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Console Random number to variable

    Trying too hard I think. Take your time and read what's posted with an open mind. Then you'll see that myRandom really does mean my Random. Read the code and consider its meaning rather than just looking at it as symbols.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member Cyberflyz's Avatar
    Join Date
    Apr 2007
    Location
    Utah
    Posts
    165

    Re: Console Random number to variable

    I think you've answer every question i've ever posted! thank you so much!

    I do have one last question...
    How do you get the program to read one character on the line at a time?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Console Random number to variable

    Quote Originally Posted by Cyberflyz View Post
    I think you've answer every question i've ever posted! thank you so much!

    I do have one last question...
    How do you get the program to read one character on the line at a time?
    That's not really anything to do with the topic of this thread, so it belongs in a new thread of its own. One thread per topic and one topic per thread please.

    Just as you're using the Console class to write data to the console window, so too you use the Console class to read input from the console window. There are several methods that can be used to read data. You should read the documentation and decide which is best for your circumstances. By reading the documentation you will get to know all of them so you will know which and when to use them in the future.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width