5 Replies - 3344 Views - Last Post: 17 October 2007 - 07:13 AM Rate Topic: -----

#1 quakeworld2007  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 71
  • Joined: 28-September 07

Help with Random Numbers

Posted 16 October 2007 - 02:24 PM

This is not Hw or anything but me trying to implement a java project into vb.net. I am having some problems with the random number. I have two dice and each time they are rolled they return the same number. What am i doing wrong.


Public Class Dice

	Dim x As Integer
	Dim y As Integer
	Dim RandomClass As New Random()

	Private faceValue1 As Integer
	Private faceValue2 As Integer

	Public Sub New()

	End Sub

	Public Function getFaceValueDice1()
		Return faceValue1
	End Function

	Public Function getFaceValueDice2()
		Return faceValue2
	End Function

	Public Sub rollDice1()
		x = RandomClass.Next(1, 7)
		faceValue1 = x
	End Sub

	Public Sub rollDice2()
		y = RandomClass.Next(1, 7)
		faceValue2 = y
	End Sub

End Class





Public Class DiceGame

	Dim die1 As Dice
	Dim die2 As Dice

	Dim value1 As Integer
	Dim value2 As Integer

	Public Sub New()
		die1 = New Dice()
		die2 = New Dice()
	End Sub

	Public Function Play() As String

		die1.rollDice1()
		die2.rollDice2()

		value1 = die1.getFaceValueDice1()
		value2 = die2.getFaceValueDice2()

		If (value1 + value2 = 7) Then
			Return "value of dice 1 is " & value1 & ControlChars.CrLf & _
		"value of dice 2 is " & value2 & ControlChars.CrLf & "Congraulations, you win with a 7"
		Else
			Return "value of dice 1 is " & value1 & ControlChars.CrLf & _
	   "value of dice 2 is " & value2 & ControlChars.CrLf & "Sorry, you lost with a value of " & (value1 + value2)
		End If

	End Function


End Class





Is This A Good Question/Topic? 0
  • +

Replies To: Help with Random Numbers

#2 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Help with Random Numbers

Posted 16 October 2007 - 05:18 PM

The only thing I can see that might throw this currently is the lack of a return type on your two functions in your Dice Class. Try rewriting them like this and see if it helps any.

	Public Function getFaceValueDice1() as Integer
		Return faceValue1
	End Function

	Public Function getFaceValueDice2() as Integer
		Return faceValue2
	End Function



However, in most cases with VB, you would actually access these variables through a property.
	Public Property FaceValue1() As Integer
		Get
			Return facevalue1
		End Get
		Set(ByVal value As Integer)
			facevalue1 = value
		End Set
	End Property

	Public Property FaceValue2() As Integer
		Get
			Return facevalue2
		End Get
		Set(ByVal value As Integer)
			facevalue2 = value
		End Set
	End Property



Although in this case, you would probably want to declare them as Public Readonly Property I provided a normal property for reference. The Readonly/Writeonly attributes just take away the Set and Get methods respectively.
Was This Post Helpful? 0
  • +
  • -

#3 quakeworld2007  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 71
  • Joined: 28-September 07

Re: Help with Random Numbers

Posted 16 October 2007 - 05:29 PM

Some reason this made it work and not roll the same two numbers...


Public Sub rollDice()

		y = Int(Rnd() * 6 + 1)

		faceValue = y

	End Sub




Anyone know why that worked and why this did not?


Public Sub rollDice1()
		x = RandomClass.Next(1, 7)
		faceValue1 = x
	End Sub




thanks
Was This Post Helpful? 0
  • +
  • -

#4 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Help with Random Numbers

Posted 17 October 2007 - 03:33 AM

Well, this is just an off the wall guess to be honest (and I expect it to be shot out of the water accordingly), but it could be because you're instantiating 2 different objects of the Random class, and they're starting on the same number in a sequence (since "random" isn't really random). As to why or how they're doing this, your guess is as good as mine, but as a test, only create one dice object, and use rolldice1 and rolldice2 from the same object.

As I said, this is only a theory, and not a very plausible one at that.
Was This Post Helpful? 0
  • +
  • -

#5 orcasquall  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 158
  • Joined: 14-September 07

Re: Help with Random Numbers

Posted 17 October 2007 - 06:52 AM

Quote

it could be because you're instantiating 2 different objects of the Random class, and they're starting on the same number in a sequence (since "random" isn't really random).

Right on target aceofspades686!

Well, random numbers generated by these standard functions aren't really random. A series of pseudo-random numbers satisfying some statistical requirement is generated. As I understand it, the numbers are repeated once the end of the series is reached (so you'll never run out of numbers...)

For the Rnd() VB function, (I believe) there's only one generated series. So a consecutive series of calls to Rnd() function generates different numbers, even if they were called from two different (Dice) classes.

The Random class however, generates its series of pseudo-random numbers at the point of creation (the constructor). There are actually two constructors, the first is the one you used (no parameters), the second takes in an integer as a seed value. Find out more here
http://msdn2.microso...tem.random.aspx

Actually the first constructor assumes a default seed value, based on a time-dependent value. This explains why
	Public Sub New()
		die1 = New Dice()
		die2 = New Dice()
	End Sub


gives you two dice with the same sequence. There's practically no time difference between the instantiation, so both die1 and die2 are created with the same seed value.

There are two methods to solve the problem. The first method is explicitly passing in different seed values
' Your Dice class constructor
Public Sub New(ByVal i As Int32)
	RandomClass = New Random(i)
End Sub

' Your DiceGame class constructor
Public Sub New()
	die1 = New Dice(102532)
	die2 = New Dice(682388)
End Sub 


This is more for educational purposes than practical ones.

The second method only requires one change
' In your Dice class, use this
Shared RandomClass As New Random
' instead of
' Dim RandomClass As New Random


The Shared keyword (static for the C family coders) means that every Dice class instantiated uses the same Random class. Just because you have two Dice, doesn't mean you need two Random classes.

This method is more elegant, and much more scalable. You can add in more dice, and the "randomness" will still be in effect. Hope this clarifies matters.

As an aside, you might want to consider allowing the Dice class to take on a variable number of sides, instead of limiting it to 6. This way, you can handle the more popular 4-, 6-, 8-, 12-, 20-sided, and even the 100-sided percentile dice. Just an opinion.
Was This Post Helpful? 0
  • +
  • -

#6 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Help with Random Numbers

Posted 17 October 2007 - 07:13 AM

Great replies! Thanks for helping out!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1