|
-
Nov 8th, 2009, 10:21 PM
#1
Thread Starter
Addicted Member
[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.
-
Nov 8th, 2009, 10:34 PM
#2
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.
-
Nov 9th, 2009, 12:01 AM
#3
Thread Starter
Addicted Member
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
-
Nov 9th, 2009, 12:10 AM
#4
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
-
Nov 9th, 2009, 12:18 AM
#5
Thread Starter
Addicted Member
Re: Console Random number to variable
Im way sorry...I feel dumb.
I don't entirely know how to use the myRandom part...
-
Nov 9th, 2009, 12:50 AM
#6
Re: Console Random number to variable
-
Nov 9th, 2009, 12:56 AM
#7
Thread Starter
Addicted Member
Re: Console Random number to variable
I got it to work. Dang, i'm way sorry...im so slow
-
Nov 9th, 2009, 01:02 AM
#8
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.
-
Nov 9th, 2009, 01:24 AM
#9
Thread Starter
Addicted Member
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?
-
Nov 9th, 2009, 01:28 AM
#10
Re: Console Random number to variable
 Originally Posted by Cyberflyz
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|