add two 6 sided dice rolls-VBForums
Results 1 to 8 of 8

Thread: add two 6 sided dice rolls

  1. #1

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

    add two 6 sided dice rolls

    VB.NET Code:
    1. Dim myRandomGenerator As System.Random
    2.         'roll1
    3.         Dim r1 As Integer
    4.         'roll2
    5.         Dim r2 As Integer
    6.         myRandomGenerator = New System.Random
    7.         Label1.Text = myRandomGenerator.Next(1, 7)
    8.         Label2.Text = myRandomGenerator.Next(1, 7)
    9.  
    10.         r1 = Val(Label1.Text) 'val()change text to number 'change what control you want (textbox, label, ect)
    11.         r2 = Val(Label2.Text) 'val()change text to number 'change what control you want(textbox, label, ect)
    12.         Label3.Text = r1 + r2 'add the two random numbers


    this is the easiest way i know of.. found a one random number generator on msdn student learning center and made a second one ^^
    Last edited by rockyamyx; Dec 2nd, 2010 at 03:44 PM. Reason: Forgot to wrap code

  2. #2
    Member raineym's Avatar
    Join Date
    Dec 2006
    Location
    Roanoke Rapids, NC
    Posts
    40

    Re: add two 6 sided dice rolls

    I'm a stickler for clean code, and you don't need to assign the randomly generated numbers to Labels and then use VAL() to convert them back to numbers so that you can add them. This does the same thing with way less overhead:

    VB Code:
    1. Dim myRandomGenerator As System.Random = New System.Random
    2. Dim r1 As Integer = myRandomGenerator.Next(1, 7)
    3. Dim r2 As Integer = myRandomGenerator.Next(1, 7)
    4. Label1.Text = r1.ToString
    5. Label2.Text = r2.ToString
    6. Label3.Text = r1 + r2
    Michael Rainey
    --------------------
    OS: Windows 7 Professional x64 SP1
    VS: 2010
    Level: Intermediate

    Controls: ImageComboBox

  3. #3

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

    Re: add two 6 sided dice rolls

    thanks for the reply.. and i appreciate your input ^^ .. and i now stand *cough corrected your way looks so simple *cough*.. lol thank you

  4. #4
    Member raineym's Avatar
    Join Date
    Dec 2006
    Location
    Roanoke Rapids, NC
    Posts
    40

    Re: add two 6 sided dice rolls

    No problem. I actually created a die roller for my gaming group. We all used laptops and it had "real-time die roll streaming" capabilities that sent the die rolls to each person connected to the host.

    I would be more than happy to send you the code. I never fully developed it so the code is really raw and our gaming group started using Fantasy Grounds.
    Michael Rainey
    --------------------
    OS: Windows 7 Professional x64 SP1
    VS: 2010
    Level: Intermediate

    Controls: ImageComboBox

  5. #5
    Lively Member
    Join Date
    Sep 2010
    Location
    Canada
    Posts
    68

    Re: add two 6 sided dice rolls

    Cleaner and type-safe code would be this:
    Code:
    Dim r As New Random()
    Dim r1 As Integer = r.Next(1, 7)
    Dim r2 As Integer = r.Next(1, 7)
    Me.Label1.Text = r1.ToString()
    Me.Label2.Text = r2.ToString()
    Me.Label3.Text = (r1 + r2).ToString()

  6. #6

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

    Re: add two 6 sided dice rolls

    Quote Originally Posted by raineym View Post
    No problem. I actually created a die roller for my gaming group. We all used laptops and it had "real-time die roll streaming" capabilities that sent the die rolls to each person connected to the host.

    I would be more than happy to send you the code. I never fully developed it so the code is really raw and our gaming group started using Fantasy Grounds.
    really?? that would be awesome dude.. id like to give it a try

  7. #7
    Member raineym's Avatar
    Join Date
    Dec 2006
    Location
    Roanoke Rapids, NC
    Posts
    40

    Re: add two 6 sided dice rolls

    Quote Originally Posted by rockyamyx View Post
    really?? that would be awesome dude.. id like to give it a try
    Just PM me your email address and its all yours.
    Michael Rainey
    --------------------
    OS: Windows 7 Professional x64 SP1
    VS: 2010
    Level: Intermediate

    Controls: ImageComboBox

  8. #8

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

    Re: add two 6 sided dice rolls

    thx again.. itll take me sum time to look at it all.. (lots and lots of code but.. at least theres lots and lots of comments lol)

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.