PDA

View Full Version : Random NUMBER



jhnnyboz
12-08-2011, 06:24 PM
hey i am trying to make a random number generator that will not choose the numbers the user has already choosen.

so the code is like

Sub game()
Dim user1 As Integer
Dim computer1 As Integer
user1 = InputBox("Pick number 1-9")
Do
computer1 = Int((9 - 1 + 1) * rnd() + 1)
Loop Until computer1 <> user1
MsgBox ("Computer picks " & computer1 & " you pick " & user1)
End Sub


but it still picks the number the user has picked ..........WHY????

p45cal
12-08-2011, 10:44 PM
but it still picks the number the user has picked ..........WHY????hey, it doesn't. You've coded correctly.

santhoshkv
12-09-2011, 03:47 AM
The reason why you got the same answer might be beacuse,the rnd() function generates new random number every time when executing the step. So there is possibility for you to get the same value of the user. Try assign the value of the rnd() to a variable and pass it in the expression. I think that might solve the above problem.
ie
Do
dblrnd = rnd()
computer1 = int((9-1+1)*dblrnd+1)
Loop until computer1 <> user1

hey i am trying to make a random number generator that will not choose the numbers the user has already choosen.

so the code is like

Sub game()
Dim user1 As Integer
Dim computer1 As Integer
user1 = InputBox("Pick number 1-9")
Do
computer1 = Int((9 - 1 + 1) * rnd() + 1)
Loop Until computer1 <> user1
MsgBox ("Computer picks " & computer1 & " you pick " & user1)
End Sub


but it still picks the number the user has picked ..........WHY????

jhnnyboz
12-09-2011, 08:49 AM
hey thanks for the info.... however, i tried that and it will still pick the same number the user picks. i dont get why tho because it should loop until they are not equal. so it seems like it is thinking they are not equal when they really are. when i highlighted the computer1 variable in VBA it shows ( say for example) 3 but when i highligh the user1 variable it shows a "3". maybe the computer thinks there are quotes around the user1 input and therefore "3" will never be equal to 3. is my logic right? and if so how do i fix this.

thakns

p45cal
12-09-2011, 09:58 AM
hey thanks for the info.... however, i tried that and it will still pick the same number the user picks. i dont get why tho because it should loop until they are not equal. so it seems like it is thinking they are not equal when they really are. when i highlighted the computer1 variable in VBA it shows ( say for example) 3 but when i highligh the user1 variable it shows a "3". maybe the computer thinks there are quotes around the user1 input and therefore "3" will never be equal to 3. is my logic right? and if so how do i fix this.

thaknshey, if the user1 variable is showing as "3" (in quotes) it's a string, so the Dim user1 as integer can't be being effective. Are you sure the code is as you've shown? Put Option Explicit at the very top of the module (not the procedure), and try running again. If you get a variable not defined error, your spelling of variables is not right somewhere.

Dim list like:
Dim user1, aser2, sommat as integer
only sommat is an integer, the rest are variants, and would be a string if assigned with inputbox.]

Edit: having just tried this, the code still works correctly even if user1 is "3" and computer1 is 3, the code thinks they are equal anyway.

jhnnyboz
12-09-2011, 10:09 AM
Hey thanks for the quick respose.... i checked the spelling everywhere and it is correct,

i put a if statement in to see if it would even recognize when user1 and computer1 are equal and even when they are equal it seems to think they are not

here is the code

Sub game()
Dim user1, computer1 as Integer
Dim results As String

user1 = InputBox("Pick number 1-9")
computer1 = Int((9 - 1 + 1) * rnd + 1)

If computer1 = user1 Then
results = "same"
Else
results = "different"
End If

MsgBox ("Computer picks " & computer1 & " you pick " & user1 & " " & results)
End Sub


even when the numbers are the same it still says results = different.

WHY?????????????


EDIT



you are correct sir.. that is what is was, the other variables were defined as variants. it works now


thanks man

p45cal
12-09-2011, 10:20 AM
as mentioned in my last post, try changing
Dim user1, computer1 as Integer
to
Dim user1 as Integer, computer1 as Integer
and see if things improve.

What version of Excel, what operating sytem, and are you on a Mac?

just seen your last edit - well done.

shrivallabha
12-09-2011, 10:23 AM
What looks to be the quick method of declaring integers can actually blow in your face.
Dim r, r1, r2, r3 as Range
actually results in r, r1, r2 being variants.
The correct method is:
Dim r as Range, r1 as Range, r2 as Range, r3 as Range

mikerickson
12-10-2011, 10:55 AM
A loop isn't needed
Sub test()
Dim maxAllowed As Long, i As Long
Dim userNumber As Long
Dim randomNumber As Long

maxAllowed = 9

userNumber = Application.InputBox("Enter a number between 1 and " & maxAllowed, Type:=1)
If userNumber = 0 Then Exit Sub: Rem canceled

Randomize
randomNumber = ((userNumber + Int(Rnd() * (maxAllowed - 1))) Mod maxAllowed) + 1

MsgBox "You chose " & userNumber & vbCr & "Computer chose " & randomNumber
End Sub