|
-
Feb 18th, 2005, 02:26 PM
#1
Thread Starter
Addicted Member
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!! =-
-
Feb 18th, 2005, 02:51 PM
#2
Re: break an integer into 3 random pieces in VB
 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.
-
Feb 20th, 2005, 06:03 AM
#3
Re: break an integer into 3 random pieces in VB
 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:
Public Sub Split3Parts(ByVal value As Integer, ByRef a As Integer, ByRef b As Integer, ByRef c As Integer)
Dim r As Random = New Random
Dim x, y, z As Integer
x = r.Next(0, value + 1)
y = r.Next(0, value + 1)
If x > y Then
z = x
x = y
y = z
End If
a = x
b = Math.Abs(x - y)
c = Math.Abs(value - y)
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.
-
Feb 20th, 2005, 06:04 AM
#4
Re: break an integer into 3 random pieces in VB
To show it works... (put this in a button click event)
VB Code:
Dim a, b, c As Integer
Split3Parts(800, a, b, c)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|