Results 1 to 4 of 4

Thread: break an integer into 3 random pieces in VB

  1. #1

    Thread Starter
    Addicted Member kill_bill_gates's Avatar
    Join Date
    Oct 2004
    Posts
    222

    Question break an integer into 3 random pieces in VB

    how can i break an integer to three random pieces(e.g. for 800; 300,225,275) in VB?
    Last edited by kill_bill_gates; Feb 18th, 2005 at 02:33 PM.
    "Quis custodiet ipsos custodes?"
    Juvenal
    Mete the Hun wanted to live in peace with the Chinese. So he gave the Chinese Emperor his favorite horse, best swords in his armory, and lots of other cool stuff. But then the Chinese Emperor asked for one thing. A useless land through the north. It was a small, useless, unproductive, uninhabited piece of land. But Mete the Hun's answer was certain:
    I gave you horses, weapons and much more which belonged to me. But the lands are not mine, it's my nation's and I'm ready to fight, kill and die for just an inch my country
    -=Joey Jordison R0CKS!! =-

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: break an integer into 3 random pieces in VB

    Quote Originally Posted by kill_bill_gates
    how can i break an integer to three random pieces(e.g. for 800; 300,225,275) in VB?
    Pick a random number from 1 to 800 - that's the first bite.

    Subtract that from the value - let's say 300 was first pick.

    Remaining value is now 500.

    Pick a random number from 1 to 500 - that's the second bite.

    Subtract that from the value - let's say it's 225 like your example.

    This leaves you with 275.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: break an integer into 3 random pieces in VB

    Quote Originally Posted by kill_bill_gates
    how can i break an integer to three random pieces(e.g. for 800; 300,225,275) in VB?
    VB Code:
    1. Public Sub Split3Parts(ByVal value As Integer, ByRef a As Integer, ByRef b As Integer, ByRef c As Integer)
    2.  
    3.         Dim r As Random = New Random
    4.         Dim x, y, z As Integer
    5.  
    6.         x = r.Next(0, value + 1)
    7.         y = r.Next(0, value + 1)
    8.  
    9.         If x > y Then
    10.             z = x
    11.             x = y
    12.             y = z
    13.         End If
    14.  
    15.         a = x
    16.         b = Math.Abs(x - y)
    17.         c = Math.Abs(value - y)
    18.  
    19.     End Sub

    Seems to work ok, inserts values BYREF into a, b and c. Doesn't neccessarily give results in numerical order.

    Can easily be modified to return a sorted array.
    I don't live here any more.

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: break an integer into 3 random pieces in VB

    To show it works... (put this in a button click event)

    VB Code:
    1. Dim a, b, c As Integer
    2.  
    3. Split3Parts(800, a, b, c)
    4.  
    5. Me.Text = a & " " & b & " " & c & " " & "(total: " & (a + b + c) & ")"
    I don't live here any more.

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