Flash Games

 FAQ   Search   Members   Groups   Register  User Control Panel      Login 

Your time now:
Mon Nov 23, 2009 2:39 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ]  Bookmark and Share
Author Message
 Post subject: Generating a random number
PostPosted: Fri Oct 03, 2008 12:24 am 
Offline

Joined: Fri Oct 03, 2008 12:15 am
Posts: 5
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:
   
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:
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2008 5:35 pm 
Offline

Joined: Sun Sep 21, 2008 11:20 am
Posts: 7
For secret number:

Code:
Option Explicit
Private SecretNumber As Integer

Private Function GetSecretNumber() As Integer

Randomize

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




Code:


Private Sub cmdGetSecretNumber_Click()

SecretNumber = GetSecretNumber()
End Sub


Now its between 1 and 6


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 04, 2008 5:07 pm 
Offline

Joined: Fri Oct 03, 2008 12:15 am
Posts: 5
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:
    Public Function Roll() As Integer
        Roll = CInt(Int(Rnd(1) * 6) + 1)
    End Function


and

Code:
someVar = Roll()


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 04, 2008 5:55 pm 
Offline

Joined: Sun Sep 21, 2008 11:20 am
Posts: 7
Code:
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?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 05, 2008 7:42 am 
Offline

Joined: Fri Oct 03, 2008 12:15 am
Posts: 5
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:
someVar = Roll()

If someVar = 0 Then
     someVar = 1
EndIf


...do the trick? This seems to be more complicated than it should be.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 05, 2008 11:03 am 
Offline

Joined: Sun Sep 21, 2008 11:20 am
Posts: 7
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:
someVar = Roll()

If someVar = 0 Then
     someVar = Roll()
EndIf


So if The number is zero, you roll again.


Hope this works!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 05, 2008 8:56 pm 
Offline

Joined: Fri Oct 03, 2008 12:15 am
Posts: 5
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:
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:
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group - Flash Games - TNX Invitation Code - TNX Review


Webmaster - Excruciating - Johnathan - Kotik - Ash - Tomi - rangana - Phate - dflynn - Medley