1

I currently Have this code in VB6.

Option Explicit

Dim RandomNum As Integer

Private Sub Form_Load()

   randomize

   Label1.Caption = RandomNum = Int((Rnd * 10) + 1)

End Sub
C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67

3 Answers3

1

Here is the answer by #steven-doggart.

"In VB6, you need to initially seed the random number generator using the Randomize function. Then, to generate a random number, you must use the Rnd function, for instance.."

Community
  • 1
  • 1
Ram Murti
  • 13
  • 7
1

You're not assigning the random value to your variable or the label correctly.

Change:

Label1.Caption = RandomNum = Int((Rnd * 10) + 1)

To:

RandomNum = Int((Rnd * 10) + 1)
If Random >= 1 And RandomNum <=3 Then
    Label1.Caption = "Option1"
Else
    Label1.Caption = "Option2"
End If

You can also look into using a Select Case statement.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Could i also just use lable1.caption = Int((Rnd * 10) + 1) – Shivam patel Nov 17 '14 at 00:14
  • Sure you could. It just seemed like you needed to store that value in your class level variable since you went thru the trouble of declaring it: `Dim RandomNum As Integer` – Idle_Mind Nov 17 '14 at 00:16
  • How would i use this number to choose an option for me. for example, if the random number generated is between 1 to 3 make the caption of a label = "Option1" and if its 4 to 6 make it "Option2". – Shivam patel Nov 17 '14 at 00:25
0

I have this function for randomizing with limits :) Hope this may help you also.

Public Function Random(Upper As Integer, Lower As Integer) As Integer
Randomize
Random = Int(Upper * Rnd() + Lower)
End Function