Results 1 to 6 of 6

Thread: [Resolved]two random Numbers

  1. #1

    Thread Starter
    Member rockyamyx's Avatar
    Join Date
    Nov 2010
    Posts
    62

    Resolved [Resolved]two random Numbers

    every time i press the Roll Dice button the turn1roll1, turn1roll2, or turn2roll1, turn2roll2(depending on turn variable) it shows system.random

    in ASCII Chart i used the label names so no confusion ^^





    _________________________________
    | turn1roll1 | turn1roll2 | turn1sum |
    | turn2roll1 | turn2roll2 | turn2sum |
    | |
    | |
    | |
    | |
    | |
    | |
    | |
    | [RollDice] |
    | |
    | |
    __________________________________





    Code:
    Public Class Form1
        Dim turn = 1
    
    
    
        Private Sub RollDice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dice.Click
    
            Dim die1 As New Random
            Dim die2 As New Random
            Dim roll As Integer = die1.Next(1, 7) + die2.Next(1, 7)
    
    
            If turn = 1 Then
                turn1roll1.Text = die1.ToString
                turn1roll2.Text = die2.ToString
                turn1sum.Text = roll.ToString
                turn = turn + 1
            ElseIf turn = 2 Then
                Dim roll2 As Integer = die1.Next(1, 7) + die2.Next(1, 7)
                turn2roll1.Text = die1.ToString
                turn2roll2.Text = die2.ToString
                turn2sum.Text = roll2.ToString
                turn = turn - 1
            End If
    
    
    
        End Sub
    Last edited by rockyamyx; Nov 26th, 2010 at 09:00 AM. Reason: Resolved

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: two random Numbers

    Hi, you are not using the Random class correctly. One instance is all you need and it can go up by your turn (I take it you are not using option strict) variable.
    Code:
    Public Class Form1
        Dim turn As Integer = 1
        Dim random As Random = new Random()
    
        Private Sub RollDice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dice.Click
    
            Dim die1 As Integer = random.Next(1, 7)
            Dim die2 As Integer = random.Next(1, 7)
            Dim roll As Integer = die1 + die2
    
            If turn = 1 Then
                turn1roll1.Text = die1.ToString
                turn1roll2.Text = die2.ToString
                turn1sum.Text = roll.ToString
                turn = 2
            Else
                turn2roll1.Text = die1.ToString
                turn2roll2.Text = die2.ToString
                turn2sum.Text = roll.ToString
                turn = 1
            End If
    
        End Sub
    W o t . S i g

  3. #3

    Thread Starter
    Member rockyamyx's Avatar
    Join Date
    Nov 2010
    Posts
    62

    Re: two random Numbers

    lol idk what that(stricked) even means.. im a pretty good newb when it comes to this .. but i'm trying

    very good help.. fixed it kudos for you ^^

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: two random Numbers

    Quote Originally Posted by MSDN
    The Visual Basic compiler provides several options for checking your code at compile time. Option Explicit determines whether variables must be explicitly declared. Option Strict determines whether explicit narrowing conversions and late binding are allowed. Option Infer enables type inference for member-level (local) variables. Option Compare specifies the method that is used for string comparisons: binary (case-sensitive) or text (case-insensitive).
    I might have got my option mixed up (I'm not a VB.Net user) I think the above are off as default and I think it is generally thought to be a good idea to switch them on. Hit F1 and have a read.
    W o t . S i g

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: two random Numbers

    Option Explicit is On by default, meaning that you can;t use a variable without declaring it first.

    Option Compare is Binary by default, meaning that the Like operator is case-sensitive by default.

    Option Infer is On by default, meaning that the compiler can infer the type of a local variables from its initialising expression.

    Option Strict is Off by default, meaning implicit narrowing conversions and late-binding are supported.

    The only change you should make is turning Option Strict On in the project properties and also in the IDE options, so all future projects have it On by default. That will disallow late-binding and implicit narrowing conversions, which will force you to write code that performs better and is less error-prone.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: two random Numbers

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim foo As New Dice() 'standard game - 2, 6 sided dice
            'Dim foo As New Dice(2, 1) 'as a coin
    
            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff"))
            Dim dt As DateTime = DateTime.Now
    
            Const sampleThrows As Integer = 1000000 'how many times to throw the dice
    
            For x As Integer = 1 To sampleThrows 'throw dice 
                foo.RollAll()
            Next
    
            Dim rolls As Long = foo.GetRolls
            Debug.WriteLine(rolls.ToString("N0") & " took " & (DateTime.Now - dt).TotalMilliseconds & " ms.")
    
            Dim stats As Dictionary(Of Long, Integer) = foo.GetStats 'get the stats
            For Each kvp As KeyValuePair(Of Long, Integer) In stats
                Debug.WriteLine(String.Format("{0} - occurred {1} times ({2})", _
                                              kvp.Key.ToString, _
                                              kvp.Value.ToString("n0"), _
                                              (kvp.Value / rolls).ToString("P2")))
            Next
        End Sub
    End Class
    
    Public Class Dice
        Private Shared PRNG As New Random 'one pseudo random generator
        Private _theDice As Dictionary(Of Integer, die) 'a collection of items
        Private _total As Long = 0 'the total for all items
        Private _keepStats As Boolean = True 'keep stats?
        Private _theStats As Dictionary(Of Long, Integer) 'the stats
        Private _rolls As Long = 0L 'number of rolls / throws / flips / etc.
    
        Public Sub New()
            'the default constructor - 2 dice, 6 faces
            Me.New(6, 2)
        End Sub
    
        Public Sub New(ByVal Faces As Integer, ByVal NumOfDie As Integer)
            If Faces < 2 OrElse NumOfDie < 1 Then Throw New ArgumentException
            Me._theDice = New Dictionary(Of Integer, die) 'create storage for the dice
            For dieNum As Integer = 1 To NumOfDie 'create a die for each
                Dim aDie As New die 'a new die
                aDie.Faces = Faces 'set the number of faces
                Me._theDice.Add(dieNum, aDie) 'add to collection
            Next
            'init stats
            Me._theStats = New Dictionary(Of Long, Integer)
            For dienum As Long = NumOfDie To NumOfDie * Faces
                Me._theStats.Add(dienum, 0) 'add bucket, count = 0
            Next
        End Sub
    
        Public Sub RollAll() 'roll all dice in collection
            Me._total = 0L 'accumulate total when rolling all
            For Each d As Integer In Me._theDice.Keys
                Me._theDice(d).CurrentFace = Dice.PRNG.Next(1, Me._theDice(d).Faces + 1)
                Me._total += CLng(Me._theDice(d).CurrentFace)
            Next
            'if keeping stats increment count
            If Me._keepStats Then
                Me._theStats(Me._total) += 1 'inc. count
                Me._rolls += 1L 'inc. roll count
            End If
        End Sub
    
        Public Sub Roll1(ByVal theDieToRoll As Integer)
            'roll a die
            Me._theDice(theDieToRoll).CurrentFace = Dice.PRNG.Next(1, Me._theDice(theDieToRoll).Faces + 1)
        End Sub
    
        ReadOnly Property Total As Long 'return total
            Get
                Return Me._total
            End Get
        End Property
    
        ReadOnly Property GetDice As List(Of die) 'return the dice
            Get
                Return Me._theDice.Values.ToList
            End Get
        End Property
    
        Property StatsOnOff As Boolean 'turn stats on / off
            Get
                Return Me._keepStats
            End Get
            Set(ByVal value As Boolean)
                'reset rolls when going from false to true
                If value = True AndAlso Me._keepStats = False Then Me._rolls = 0L
                Me._keepStats = value
            End Set
        End Property
    
        ReadOnly Property GetStats As Dictionary(Of Long, Integer) 'return the stats
            Get
                Return Me._theStats
            End Get
        End Property
    
        ReadOnly Property GetRolls As Long 'return the rolls
            Get
                Return Me._rolls
            End Get
        End Property
    End Class
    
    Public Class die
        Private _faces As Integer = -1 'the number of faces this die
        Private _curFace As Integer = -1 'the current face of this die
    
        Public Property Faces As Integer
            Get
                Return Me._faces
            End Get
            Set(ByVal value As Integer)
                Me._faces = value
            End Set
        End Property
    
        Public Property CurrentFace() As Integer
            Get
                Return Me._curFace
            End Get
            Set(ByVal value As Integer)
                Me._curFace = value
            End Set
        End Property
    End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width