[VB2008] Random selection

Newbie Spellweaver
Joined
May 17, 2008
Messages
8
Reaction score
0
I'm doing some extra credit for my programming class, and I was wondering how would you code like a random selection? I'm supposed to make a rock, paper, scissors game. The user chooses one of the three choices, and the computer makes a random selection. Thanks for your help
 
Find out how to generate a random number from 1 to 3 and create 3 if statements

if(random == 1){
rock
}
else if(random == 2){
paper
}
else if(random == 3){
scissor
}

psuedo-code and obviously not vb, same principle though
 
Randomize
RandomNumber = INT((3+1) * RND)

If INT = 1 Then
*code for scissors goes here
ElseIf INT = 2 Then
*code for paper
Else
*code for rock
End If

I think that'd work o.o

To explain the code so you learn something:

Randomize seeds the RND (random number)
RandomNumber = INT((3+1) * RND) will give you a random number between 1 and 3 every time the code is activated.

The rest is basic if/then/elseif/else statements. Pretty simple really.


I only program in VB6 but it's pretty much the same coding...

you can also change RandomNumber to be whatever you want
 
Randomize function, make it return a number between 1 and 3.

Maybe:
http://msdn.microsoft.com/en-us/library/8zedbtdt(VS.80).aspx
Dim n As Integer = CInt(Int((3 * Rnd()) + 1))

Select Case n
Case 1
MsgBox "Rock"
Case 2
MsgBox "Scissor"
Case 3
MsgBox "Paper"
Case Else
MsgBox "An error occured!.", vbCritical
End Select

Bit unsure if it works in VB2008

Edit: You should use case instead of elseif. Case is prettier.
 
Randomize function, make it return a number between 1 and 3.

Maybe:
http://msdn.microsoft.com/en-us/library/8zedbtdt(VS.80).aspx
Dim n As Integer = CInt(Int((3 * Rnd()) + 1))

Select Case n
Case 1
MsgBox "Rock"
Case 2
MsgBox "Scissor"
Case 3
MsgBox "Paper"
Case Else
MsgBox "An error occured!.", vbCritical
End Select

Bit unsure if it works in VB2008

Edit: You should use case instead of elseif. Case is prettier.
He is right... Select case looks much better but I never knew how to use it... I recommend using his method... (changes his current project to Select Case's):P:
 
Back