Re: "how to split a string in a random way"
- From: "Tom Leylan" <gee@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 15 Nov 2006 22:17:52 -0500
A couple of comments since (as I hoped) you were willing to play with the
code.
You correctly changed the variable to Long in 4 places actually. You missed
one change though, the CInt() to CLng().
I am inserting a new copy (this will be the last one). I changed the name
of FactorSet() to GenArray() to better reflect what it does, renamed a
variable from tmp to val to better illustrate it is one of the values in the
array. I also fixed the "+" symbol problem. I also included a few comments
which may help you understand things.
The parts are all present and the only one you really need to use is
Factorize(). The others are helpers just to make your job easier. One
generates an integer arrays of some specified length and the other shuffles
integer arrays. You call these if you want to (and you probably will want
to.)
The is the basic call, I created an integer array and I pass it along with a
number to Factorize(). Obviously that produces only a single result.
..
Dim array1 As Integer() = {0, 1, 2, 3, 4, 5, 6}
Debug.WriteLine("Array1: " + Utility.Factorize(123456, array1))
This time I send array1 through the Shuffle() method just prior to sending
it to Factorize. It will be different each time because the single array
repeatedly gets shuffled into a new order.
Debug.WriteLine("Shuffle1: " + Utility.Factorize(123456,
Utility.Shuffle(array1)))
You can create a shorter array (if the number is small or you don't want the
larger factors of 10).
Dim array2 As Integer() = {2, 0, 1}
Debug.WriteLine("Shuffle2: " + Utility.Factorize(123456,
Utility.Shuffle(array2)))
And finally rather than generate an array I've used GenArray() to do that in
this example.
Dim array3 As Integer() = Utility.GenArray(5)
Debug.WriteLine("Shuffle3: " + Utility.Factorize(123456,
Utility.Shuffle(array3)))
And just to prove it can all be run together I'm generating an array,
passing it to Shuffle and then passing it to Factorize.
Debug.WriteLine("Shuffle4: " + Utility.Factorize(123456,
Utility.Shuffle(Utility.GenArray(5))))
You wrote:
x = Utility.RndGen.Next(999999, 9999999)
You can call the random number generator if you want to but it has no affect
on the methods I wrote. It is there if you need it for other things.
Similarly see the notes on Shuffle() and GenArray() which you can use in
other situations.
You wrote:
TextBox1.Text += x.ToString ' (Utility.Factorize(x, Utility.FactorSet(0)))
doesn't work to show just the number
FactorSet() (now named GenArray()) really can't accept the value 0. It
doesn't fail but it produces an empty array which ultimately produces an
empty string. Maybe it makes more sense now that it is called GenArray().
You wrote:
if melange = true then....
'here i think it's the right place to buid a new array of (5*100), (4*10),
(2*1) so i can shuffle it in a new order and built result with a loop
End If
Don't going "melanging" on me :-) If you want to melange the code add
another method. Remember if you have 1 method that does everything you can
only have it do that one thing. If on the other hand you have 5 or 6
methods that can interact you can combine them into new methods. If you
want a method to create, shuffle and factorize values just create one but
use the existing methods to do it.
Public Shared Function Melange(ByVal num As Long) As String
Return Factorize(num, Shuffle(GenArray(7))))
I didn't test it but this should return a newly shuffled version of the
factored string each time you call it.
You wrote:
C) I wonder if i have to change it in the shuffle function : we use the
"word" : order byref, also here, so ?
Generally you don't want to change anything that exists, just add more
methods if you need them. You can fix errors of course but try not to
change the base functionality. Certainly resist the idea of making
Shuffle() do something like updating the screen or Factorize() doing
anything except factor the number. My mistake on the ByRef (I've changed
it) I knew I typed that but I forgot to go back to fix it the first time.
The array passed in is changed but it returns the array reference as well to
make the nesting of method calls possible as well.
You wrote:
D) I feel so small reading this code : only few line to make a huge job !
The function shuffle amazes me !
E) Sure If you come in France next day, you can have cheese and wine at
home , it's not a problem !
That's why I like algorithms... :-) Okay then a nice brie and a bottle of
Cotes du' Rhone.
Here is the code I don't want to see it any more :-)
Dim array1 As Integer() = {0, 1, 2, 3, 4, 5, 6}
Dim array2 As Integer() = {2, 0, 1}
Dim array3 As Integer() = Utility.GenArray(5)
Debug.WriteLine("Array1: " + Utility.Factorize(123456, array1))
Debug.WriteLine("Shuffle1: " + Utility.Factorize(123456,
Utility.Shuffle(array1)))
Debug.WriteLine("Array2: " + Utility.Factorize(123456, array2))
Debug.WriteLine("Shuffle2: " + Utility.Factorize(123456,
Utility.Shuffle(array2)))
Debug.WriteLine("Array3: " + Utility.Factorize(123456, array3))
Debug.WriteLine("Shuffle3: " + Utility.Factorize(123456,
Utility.Shuffle(array3)))
Public Class Utility
Inherits Object
' this is a random number generator and is used by the Shuffle() method
' you can reference it from your code any time you need a random number
' using it in other places will effectively have zero impact on the
Shuffle() method
Public Shared RndGen As System.Random = New System.Random
Public Shared Function Factorize(ByVal num As Long, ByVal order As
Integer()) As String
' Factorize returns a string that represents the value passed as num
in the form of an
' equation made up of factors of 10
' 123456 could be returned as: (123 * 1000) + (456 * 1)
' or 123456 could be returned as: (1234 * 100) + (5 * 10) + (6 * 1)
' the format is controlled by an integer array named order
Dim Result As String = ""
Dim res(order.Length - 1) As Long
order.CopyTo(res, 0)
Dim div(order.Length - 1) As Long
order.CopyTo(div, 0)
Dim bal As Long = num
For i As Integer = 0 To (order.Length - 1)
Dim val As Integer = order(i)
div(val) = CLng(10 ^ val)
res(val) = (bal \ div(val))
bal -= (div(val) * res(val))
Next
For i As Integer = (order.Length - 1) To 0 Step -1
If (res(i) > 0) Then
If (Result.Length > 0) Then Result += " + "
Result += "(" + res(i).ToString + " * " + div(i).ToString +
")"
End If
Next
Return Result
End Function
Public Shared Function GenArray(ByVal max As Integer) As Integer()
' returns an Integer array with max elements
' each element is initialized with a value equal to it's element
number
' so GenArray(3) will return {0, 1, 2}
' obviously you can create such an array without GenArray() but this
can be a bit easier
' the GenArray() method can be called on for any purpose and can
easily generate
' a 52-element array representing (for instance) a deck of cards
(see: Shuffle)
Dim Result(max - 1) As Integer
For i As Integer = 0 To (Result.Length - 1)
Result(i) = i
Next
Return Result
End Function
Public Shared Function Shuffle(ByVal order As Integer()) As Integer()
' returns an Integer array passed to it as order after shuffling the
elements
' the Shuffle() method can shuffle any Integer array (for any
purpose) and can easily
' shuffle a 52-element array representing (for instance) a deck of
cards (see: GenArray)
' please note that Shuffle() need only be called once (an array is
shuffled or it isn't)
' there is nothing to be gained by shuffling twice
Dim rnd As Integer
Dim val As Integer
Dim max As Integer = (order.Length - 1)
For i As Integer = 0 To max
val = order(i)
rnd = RndGen.Next(0, max + 1)
order(i) = order(rnd)
order(rnd) = val
Next
Return order
End Function
End Class
.
- Follow-Ups:
- References:
- "how to split a string in a random way"
- From: Pascal
- Re: "how to split a string in a random way"
- From: Tom Leylan
- Re: "how to split a string in a random way"
- From: Pascal
- "how to split a string in a random way"
- Prev by Date: Re: Default Exception Value
- Next by Date: Re: Unable to remove Beep on Alt+A
- Previous by thread: Re: "how to split a string in a random way"
- Next by thread: Master are you here ... ? For a Few pieces of cheese More ;0)
- Index(es):
Relevant Pages
|