d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Vb Question
Add Reply New Topic New Poll
Member
Posts: 3,188
Joined: Jul 27 2014
Gold: Locked
Trader: Scammer
Sep 22 2014 02:11pm
What I'm trying to do is assign 2 different random variables to 2 different labels. The variables are supposed to be between 1 and 10. When I click the button to generate the random numbers, they are always the same, why is that? It's driving me nuts lol.

Code
Option Explicit On
Option Infer Off
Option Strict On

Public Class Form1

' class-level variable
Private randomInteger1 As Integer
Private randomInteger2 As Integer

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

End Sub

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
Me.Close()
End Sub

Private Sub resetButton_Click(sender As Object, e As EventArgs) Handles resetButton.Click
' prepare to assign new random numbers

Dim randGen1 As New Random
Dim randGen2 As New Random

' generates 2x random numbers from 1 to 10

randomInteger1 = randGen1.Next(1, 11)
randomInteger2 = randGen2.Next(1, 11)

' display new values

number1Label.Text = CStr(randomInteger1)
number2Label.Text = CStr(randomInteger2)
Member
Posts: 3,188
Joined: Jul 27 2014
Gold: Locked
Trader: Scammer
Sep 22 2014 02:58pm
fixed this part, just the same numbers now :/

This post was edited by nikesins on Sep 22 2014 03:03pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 22 2014 04:46pm
when you say the same, i assume you mean randomInteger1 = randomInteger2, but the values change?

i suggest you read the documentation, in particular the seed value:

http://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx

you can either change the seed for the second generator or get rid of the second generator altogether and just use 1 generator
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 23 2014 10:38am
Set the seed to getTickCount() for easiest results.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll