[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.
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.
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
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:
Dim var1 As Integer
Dim var2 As Integer
var1 = myRandom.Next(min, max)
var2 = myRandom.Next(min, max)
or you can use an array:
vb.net Code:
Dim arr(count) As Integer
For i = 0 To count - 1
arr(i) = myRandom.Next(min, max)
Next
Re: Console Random number to variable
Im way sorry...I feel dumb.
I don't entirely know how to use the myRandom part...
Re: Console Random number to variable
Re: Console Random number to variable
I got it to work. Dang, i'm way sorry...im so slow
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.
Re: Console Random number to variable
I think you've answer every question i've ever posted! :D 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?
Re: Console Random number to variable
Quote:
Originally Posted by
Cyberflyz
I think you've answer every question i've ever posted! :D 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.