4 Replies - 2883 Views - Last Post: 29 November 2008 - 05:09 PM Rate Topic: -----

#1 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

VB6 Random Number Generator Issue

Posted 29 November 2008 - 12:59 PM

I'm using some random number code and it keeps returning values with a high decimal point. (1.084106000 instead of 1000 [the .084106is the issue]). Here is the code, see if you can figure it out, I can't.

Public Sub 2D8x10()
a = ((8 - 1 + 1) * Rnd + 1)
b = ((8 - 1 + 1) * Rnd + 1)
intNum = ( a + b ) & "0"
End Sub
Private Sub cmaCalculate01_Click()


txtCash.Text = ""
txtGoods.Text = ""
txtItems.Text = ""
lblCashD00.Caption = ""
lblGoodsD00.Caption = ""
lblItemsD00.Caption = ""

lblCashD00.Caption = Int((100 - 1 + 1) * Rnd + 1)
lblGoodsD00.Caption = Int((100 - 1 + 1) * Rnd + 1)
lblItemsD00.Caption = Int((100 - 1 + 1) * Rnd + 1)
If lblCashD00.Caption <= 14 Then
    txtCash.Text = "You have found no coins."
    ElseIf lblCashD00.Caption >= 96 Then
    txtCash.Text = "You have found " & ((4 - 1 + 1) * Rnd + 1) & "0 #"
    ElseIf lblCashD00.Caption >= 53 Then
    Call 2D8x10
    txtCash.Text = "You have found " & intNum & " #"
    ElseIf lblCashD00.Caption >= 30 Then
    txtCash.Text = "You have found " & ((8 - 1 + 1) * Rnd + 1) & " #"
    ElseIf lblCashD00.Caption >= 15 Then
    txtCash.Text = "You have found " & ((6 - 1 + 1) * Rnd + 1) & "000 #"
End If

If lblGoodsD00.Caption <= 90 Then
    txtGoods.Text = "You have found nothing of value."
    ElseIf lblGoodsD00.Caption >= 96 Then
    txtGoods.Text = "You have found a rare work of art!"
    ElseIf lblGoodsD00.Caption >= 91 Then
    txtGoods.Text = "You have found a Gem!"
End If

If lblItemsD00.Caption <= 71 Then
    txtItems.Text = "You have found nothing of value."
    ElseIf lblItemsD00.Caption >= 96 Then
    txtItems.Text = "You have found a Something Good!"
    ElseIf lblItemsD00.Caption >= 72 Then
    txtItems.Text = "You have found a something."
End If

intNum = txtValue.Text



End Sub


This post has been edited by Jaalenn: 29 November 2008 - 01:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: VB6 Random Number Generator Issue

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4312
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: VB6 Random Number Generator Issue

Posted 29 November 2008 - 01:21 PM

That is because of the Rnd which returns a floating point number. Used in the calculation it makes the whole thing into floating point values and returns the float. You can simply throw the equation into a CInt statement to make it an integer and drop the decimal places.

Then you will be good to go. :)
Was This Post Helpful? 1
  • +
  • -

#3 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: VB6 Random Number Generator Issue

Posted 29 November 2008 - 05:00 PM

Martyr, you're a saint! Thanx alot man. Now to find a way to make it return a value other than the maximum.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4312
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: VB6 Random Number Generator Issue

Posted 29 November 2008 - 05:04 PM

Glad I could help out. One other tip you may want to look into is the randomize function which will seed your random number generator so it will give you better random values. Not the same random values each time you start it up.

You might do that already and don't see it, but just in case you don't know about it it would be worth looking into.

:)
Was This Post Helpful? 0
  • +
  • -

#5 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: VB6 Random Number Generator Issue

Posted 29 November 2008 - 05:09 PM

Wow, that was a fast reply. I'll do just that immediately and let you know how it worked!!


By the way... It's not letting me thank you for some reason... That's twice that's happened. I like to show some appreciation to those that help!

This post has been edited by Jaalenn: 29 November 2008 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1