It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Partner Sites
Board index Programming Visual Basic Forum

Generating a random number

Moderator: dafunkymunky

Generating a random number

Postby milo896 on Fri Oct 03, 2008 12:24 am

Hello.


I'm pretty new to VB, so I'm starting off by making a simple program. I'm trying to simulate rolling some dice and calculating various results. My problem is this: sometimes, the number generated is zero. This plays havoc with the rest of the program.


I've defined a new function to make generating my random numbers easier.


Code: Select all
   
Public Function Random(ByVal Low As Long, ByVal High As Long) As Integer
        Random = CInt(Int((High - Low + 1) * Rnd()) + Low)
    End Function



Then, whenever I want to "roll the dice," I just type something like...


Code: Select all
someVar = Random(1, 6)



This sometimes spits out a zero. From my limited understanding of how Rnd() works, this shouldn't be happening. Can anyone help?

(And yes, I'm initializing Randomize() at the start of the program.)



EDIT--

I'm using VB 2008 Express.
milo896
 
Posts: 5
Joined: Fri Oct 03, 2008 12:15 am

Postby fifarunnerr on Fri Oct 03, 2008 5:35 pm

For secret number:

Code: Select all
Option Explicit
Private SecretNumber As Integer

Private Function GetSecretNumber() As Integer

Randomize

GetSecretNumber = Int(Rnd(1) * 6) + 1
End Function




Code: Select all


Private Sub cmdGetSecretNumber_Click()

SecretNumber = GetSecretNumber()
End Sub


Now its between 1 and 6
fifarunnerr
 
Posts: 7
Joined: Sun Sep 21, 2008 11:20 am

Postby milo896 on Sat Oct 04, 2008 5:07 pm

This is very annoying. It's still giving me zeros now and then. I copied and pasted your code, and it's still not cooperating. I don't get it. Shouldn't this be working?

Code: Select all
    Public Function Roll() As Integer
        Roll = CInt(Int(Rnd(1) * 6) + 1)
    End Function


and

Code: Select all
someVar = Roll()
milo896
 
Posts: 5
Joined: Fri Oct 03, 2008 12:15 am

Postby fifarunnerr on Sat Oct 04, 2008 5:55 pm

Code: Select all
Option Explicit
Private SecretNumber As Integer

Private Function GetSecretNumber() As Integer

Randomize

GetSecretNumber = Int(Rnd(1) * 6) + 1
End Function


Private Sub Command1_Click()
SecretNumber = GetSecretNumber
Text1.Text = SecretNumber

End Sub

Private Sub Form_Load()

End Sub


Use this codes, if i only use this codes, and i add text1 and Command1 then this works...

I never get a zero, only 1,2,3,4,5,6


Meaby the reason is that i have VB 6.0?
fifarunnerr
 
Posts: 7
Joined: Sun Sep 21, 2008 11:20 am

Postby milo896 on Sun Oct 05, 2008 7:42 am

Hmm. Well, I can't see what I'm doing wrong. It looks like we're using the same code, just getting different results. I don't know if me using VB 2008 Express has anything to do with it or not. I just started learning VB a few weeks ago, so I'm far from knowing the ins and outs of the different versions.

Back to the zeros though. I added an If/Then statement that's supposed to set the variable to 1 if it does happen to roll a zero. But it's not working. Zeros are still getting generated and passed into the rest of the program. Correct me if I'm wrong, but shouldn't...

Code: Select all
someVar = Roll()

If someVar = 0 Then
     someVar = 1
EndIf


...do the trick? This seems to be more complicated than it should be.
milo896
 
Posts: 5
Joined: Fri Oct 03, 2008 12:15 am

Postby fifarunnerr on Sun Oct 05, 2008 11:03 am

Thats a correct code, what is working, BUT:
Now the chance to get NO. 1 is 2 times as big as NO 2-6.

So meaby you need somehting like:



Code: Select all
someVar = Roll()

If someVar = 0 Then
     someVar = Roll()
EndIf


So if The number is zero, you roll again.


Hope this works!
fifarunnerr
 
Posts: 7
Joined: Sun Sep 21, 2008 11:20 am

Postby milo896 on Sun Oct 05, 2008 8:56 pm

Hmm. It appears to be fixed. I don't think the problem was with generating the random number after all. I was using images of each side of the die, instead of just sticking the numbers onscreen. I stored the locations of the images in an array, and then used the random number to decide which image to use. Like this...


Code: Select all
dim dice(5) as image 'assigned image locations on form load

Public Function Random(ByVal Low As Integer, ByVal High As Integer) As Integer
     Random = CInt(Int((High - Low + 1) * Rnd()) + Low)
End Function

'more stuff here

someVar = Random(1, 6)

picturebox.Image = dice(someVar - 1)



The image paths are stored in the array in order. So the first is 1, the second is 2, and so on.

My thinking was that if the number generated was a 4, all I had to do was subtract 1 to get the index of the proper image in the array. Apparently that doesn't work. I kept getting "array out of bounds" exceptions. When I looked at the error details, it said that someVar had a value of zero. So subtracting 1 would have the program trying to access dice(-1), which of course doesn't exist. I changed the code to the following, and no more errors:


Code: Select all
someVar = Random(0, 5)

picturebox.Image = dice(someVar)



I don't understand one thing. Shouldn't I be able to use a simple expression such as (someVar - 1) as the index of an array? Did I just apply it incorrectly?
milo896
 
Posts: 5
Joined: Fri Oct 03, 2008 12:15 am


Return to Visual Basic Forum

Who is online

Users browsing this forum: No registered users and 0 guests