By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

"how to split a string in a random way"

P: n/a


hello and soory for my english
here is the query :"how to split a string in a random way"
I try my first shot in vb 2005 express and would like to split a number in
several pieces in a random way without success.
for example if the number is 123 456 : i would like to have some random
strings like theese :
(12 * 10 000) + (345 * 10) + (6*1)
or
(123*1 000)+(4*100)+(5*10)+(6*1)
etc...

i tried this :
Dim min As Long = 9999999
Dim max As Long = 999999999999
Dim Nbchi As Short = 0 'how many digit ?
Dim NbMorcMax As Short = 0 'how many pieces max ?
Dim NbMorcMin As Short = 0
Dim TailleMorcMax As Short = 0 'lenght of the pieces ?
Dim TailleMorcMin As Short = 0
'on tire un nombre

Do
NbreTire = Math.Round(objRandom.Next(min, max) +
objRandom.NextDouble(), 3)
Loop Until NbreTire 0
' on calcule le nombre de chiffres de la partie entière
Nbchi = Len(Int(NbreTire))
'peupler un tableau avec les éléments de la partie entière
Dim TabDecomposition(-1) As String
Dim i As Short = 0
Dim j As Short = 0
Dim q As Short = 0
q = Math.Round(objRandom.Next(1, Nbchi))
Dim PartieEntiere As Long = Int(NbreTire)
Do
TabDecomposition(j) = "( " & Mid$(PartieEntiere, i + 1, q) & " *
" & Format(10 ^ (Nbchi - q), "#,#") & " ) "

j += 1
i += q
Loop Until q = Nbchi
but this is not the good way because in the loop q = Nbchi is very rare... i
have to be very lucky...
so, i think about an array of that number 123 456 cut into pieces so i
should know how many pieces and so how many loop i have to do

hope you understand what i mean

can you help
thank you from Normandy
pascal
Nov 11 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Hi Pascal:

You don't quite want it done "randomly" rather you want a permutation. You
want to randomly choose one of the fixed number of permutations. (if that
makes sense.)

I hope I read it correctly that that what you want is those factored strings
as a result because I spent some time writing this. I get a kick out of
writing algorithms so it was fun but I'm not sure I'll ever pull this one
out of my library of algorithms to use again :-)

I've created a Class Utility with a shared method called Factorize. It
expects two arguments a number and an array of integers that represent the
"permutation" that you want. By having you pass in the permutation (rather
than the routine choose one) you can repeat the sequence by simply using the
same permutation. Your job will be to produce a short routine to generate
the array of integers in a random way. And as a sidenote you don't call the
random number function repeatedly to do this, you are in essence just
"shuffling" the values 0 through 5 or 6. So look up a shuffle algorithm.

You call it like this:

Dim order1 As Integer() = {0, 1, 2, 3, 4, 5, 6}
Debug.WriteLine(Utility.Factorize(123456, order1))

Dim order2 As Integer() = {6, 5, 4, 3, 2, 1, 0}
Debug.WriteLine(Utility.Factorize(123456, order2))

Dim order3 As Integer() = {5, 6, 2, 3, 4, 1, 0}
Debug.WriteLine(Utility.Factorize(123456, order3))

Dim order4 As Integer() = {2, 0, 1}
Debug.WriteLine(Utility.Factorize(3456, order4))

The values represent the successive powers of 10 so 0 represents 10 ^ 0 or
1, 10 ^ 1 = 10, etc. Note that the number of places is variable, in the
last example it is a small number so I know that 10 ^ 2 = 1000 is all that
is needed. While I could send {3, 2, 0, 1} or { 2, 5, 4, 3, 1, 0 } the exta
powers of 10 will never come back with a value and won't appear in the
returned string.

Hopefully this works for you all you have to do now is generate the "order"
array. It should be a unique set (don't send 4 twice for instance). I
think you can skip even skip a number in the sequence and it should still
work. The combination you get back is a direct result of the order you send
to the Factorize() method because it does the math in the order you are
specifying.

If this is a class assignment, mention me to the teacher I could use the
course credit :-)

Tom
Public Class Utility
Inherits Object

Public Shared Function Factorize(ByVal num As Integer, ByVal order As
Integer()) As String

Dim Result As String = ""

Dim res(order.Length - 1) As Integer
order.CopyTo(res, 0)

Dim div(order.Length - 1) As Integer
order.CopyTo(div, 0)

Dim bal As Integer = num

For i As Integer = 0 To (order.Length - 1)
Dim val As Integer = order(i)
div(val) = CInt(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
Result += "(" + res(i).ToString + " * " + div(i).ToString +
")" _
+ IIf(i 0, " + ", "").ToString
End If
Next

Return Result

End Function

End Class


"Pascal" <sc*********@wanadoo.rfwrote in message
news:45***********************@news.orange.fr...
>

hello and soory for my english
here is the query :"how to split a string in a random way"
I try my first shot in vb 2005 express and would like to split a number in
several pieces in a random way without success.
for example if the number is 123 456 : i would like to have some random
strings like theese :
(12 * 10 000) + (345 * 10) + (6*1)
or
(123*1 000)+(4*100)+(5*10)+(6*1)
etc...

i tried this :
Dim min As Long = 9999999
Dim max As Long = 999999999999
Dim Nbchi As Short = 0 'how many digit ?
Dim NbMorcMax As Short = 0 'how many pieces max ?
Dim NbMorcMin As Short = 0
Dim TailleMorcMax As Short = 0 'lenght of the pieces ?
Dim TailleMorcMin As Short = 0
'on tire un nombre

Do
NbreTire = Math.Round(objRandom.Next(min, max) +
objRandom.NextDouble(), 3)
Loop Until NbreTire 0
' on calcule le nombre de chiffres de la partie entière
Nbchi = Len(Int(NbreTire))
'peupler un tableau avec les éléments de la partie entière
Dim TabDecomposition(-1) As String
Dim i As Short = 0
Dim j As Short = 0
Dim q As Short = 0
q = Math.Round(objRandom.Next(1, Nbchi))
Dim PartieEntiere As Long = Int(NbreTire)
Do
TabDecomposition(j) = "( " & Mid$(PartieEntiere, i + 1, q) & "
* " & Format(10 ^ (Nbchi - q), "#,#") & " ) "

j += 1
i += q
Loop Until q = Nbchi
but this is not the good way because in the loop q = Nbchi is very rare...
i have to be very lucky...
so, i think about an array of that number 123 456 cut into pieces so i
should know how many pieces and so how many loop i have to do

hope you understand what i mean

can you help
thank you from Normandy
pascal


Nov 12 '06 #2

P: n/a
bonjour Tom and thanks a lot for this job!
I spent my sunday on it, trying to do what you said. So i found on the net a
function that i joined to the class Utility to make an array of integer.
i put a textbox and a button on a form to test. But there is some problems
due to type conversion or something like that if i understood the alert in
vb2005 express debogger....
the function to generate random seed of integers works fine, your function
too but after spending time to making them working togather :
I give up fot tonight !
my knoledge in vb is not enough efficient to find the good way... how to put
the number of one array built with : Utility.RandomNumbers(6, 1, 6) into
your function ?
i think it's a problem of conversion solved by the use of ctype ? But how ?

I need a little help more please.

ps : it's not a job class, it's me the teacher..... for pupils under 10. I
try to improve old stuff made in vb6 that you can see here
http://www.scalpa.info/decomposer.php to make them more efficient in maths..

THANKS for ALL

"Tom Leylan" <ge*@iamtiredofspam.coma écrit dans le message de news: %

here is the code in form1:

Private Sub BtnFactorise_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnFactorise.Click

Dim x As Integer()

Dim order1 As Integer() '= {2, 0, 1, 3, 4, 5, 6}

x = CType(Utility.RandomNumbers(6, 1, 6), Integer())

order1 = CType(Utility.RandomNumbers(6, 1, 6), Integer())

'Debug.WriteLine(Utility.Factorize(123456, order1))

TxtBxReponse.Text = Utility.Factorize(123456, order1)

End Sub
the code in the class :

Public Class Utility
Inherits Object

Public Shared Function Factorize(ByVal num As Integer, ByVal order As
Integer()) As String

Dim Result As String = ""

Dim res(order.Length - 1) As Integer

order.CopyTo(res, 0)

Dim div(order.Length - 1) As Integer

order.CopyTo(div, 0)

Dim bal As Integer = num

For i As Integer = 0 To (order.Length - 1)

Dim val As Integer = order(i)

div(val) = CInt(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

Result += "(" + res(i).ToString + " * " + div(i).ToString + ")" _

+ IIf(i 0, " + ", "").ToString

End If

Next

Return Result

End Function
Public Shared Function RandomNumbers(ByVal Upper As Integer, _

Optional ByVal Lower As Integer = 1, _

Optional ByVal HowMany As Integer = 1, _

Optional ByVal Unique As Boolean = True) As Object

'************************************************* ******

'This Function generates random array of

'Numbers between Lower & Upper

'In Addition parameters can include whether

'UNIQUE values are required

'Note the Result is INCLUSIVE of the Range

'Debug Example:

'x = RandomNumbers(49, 1, 7)

'For n = LBound(x) To UBound(x): Debug.Print x(n);: Next n

'WARNING HowMany MUST be greater than (Higher - Lower)

'************************************************* *****

On Error GoTo LocalError

If HowMany ((Upper + 1) - (Lower - 1)) Then Exit Function

Dim x As Integer

Dim n As Integer

Dim arrNums() As Object

Dim colNumbers As New Collection

ReDim arrNums(HowMany - 1)

With colNumbers

'First populate the collection

For x = Lower To Upper

..Add(x)

Next x

For x = 0 To HowMany - 1

n = RandomNumber(0, colNumbers.Count + 1)

arrNums(x) = colNumbers(n)

If Unique Then

colNumbers.Remove(n)

End If

Next x

End With

colNumbers = Nothing

RandomNumbers = arrNums

Exit Function

LocalError:

'Justin (just in case)

RandomNumbers = ""

End Function

Public Shared Function RandomNumber(ByVal Upper As Integer, _

ByVal Lower As Integer) As Integer

'Generates a Random Number BETWEEN the LOWER and UPPER values

Randomize()

RandomNumber = Int((Upper - Lower + 1) * Rnd() + Lower)

End Function

End Class
Nov 12 '06 #3

P: n/a
Pascal: I fixed the RandomNumbers function to eliminate the error but there
is something wrong with the algorithm in any case (and it was VB6 style
which I believe we would do well to avoid). Watching the output I got
duplicate zero values and even the number 7 when the range was 1 through 6.
In any case this is more of a random number generator and that isn't what
you need. You need specific numbers permutated and for that we use a
shuffle algorithm.

So to the Public Utility class add the following property.

Public Class Utility
Inherits Object

Public Shared RndGen As System.Random = New System.Random

Ordinarily I wouldn't make it public but this works and you can get to it
via the Utility class if you need random numbers in the future. Note that
it is a random number generating object and we will use it to give us random
positions in the shuffle algorithm.

Add the following Shuffle() method which shuffles Integer arrays. It makes
a single pass so it is fast and it shuffles what it is passed to. If you
create an array with 52 elements and send it to Shuffle() it is essentially
shuffling a deck of cards.

Public Shared Function Shuffle(ByRef order As Integer()) As Integer()

Dim rnd As Integer
Dim tmp As Integer

Dim max As Integer = (order.Length - 1)

For i As Integer = 0 To max
rnd = RndGen.Next(0, max + 1)
tmp = order(i)
order(i) = order(rnd)
order(rnd) = tmp
Next

Return order

End Function

To make the Factorize() method easier to use add a helper method to generate
an array of integers then you only need to tell it how many values you need.

Public Shared Function FactorSet(ByVal max As Integer) As Integer()

Dim Result(max - 1) As Integer

For i As Integer = 0 To (Result.Length - 1)
Result(i) = i
Next

Return Result

End Function
Now it can be called from your button click event as follows:

Debug.WriteLine(Utility.Factorize(123456, Utility.FactorSet(6)))

That sends the array {0,1,2,3,4,5} every time however, remember FactorSet
only creates an array with the needed values.

To add the randomization call it like this:

Debug.WriteLine(Utility.Factorize(123456,
Utility.Shuffle(Utility.FactorSet(6))))

If you are going to call it repeatedly using the same FactorSet there is
obviously no reason to recreate it over and over so simply define it
somewhere like this.

Dim i() As Integer = Utility.FactorSet(6)
Debug.WriteLine(Utility.Factorize(123456, Utility.Shuffle(i)))

And finally you can add other helper methods to the Utility class if the
most common thing to change is the value you are sending. Add something
like a RndFactorize6() method which sets up it's own FactorSet() and
shuffles it and you can simply call it as follows:

Debug.WriteLine(Utility.RndFactorize6(123456))

..So now you have all the parts which you can mix and match. You can get
random integers using Utility.RndGen.Next(), between a range using
Utility.RndGen.Next(1,10), etc. if you need them for something else. You
can shuffle any integer array, etc.

Finally in hindsight this all should have been written using Long values
instead. It would give you a larger range without losing anything in the
process. I'll leave that as an exercise for you...

Next time I'm in Normandy I'll expect wine and cheese!

Good luck.
Tom

"Pascal" <sc*********@wanadoo.rfwrote in message
news:45***********************@news.orange.fr...
bonjour Tom and thanks a lot for this job!
I spent my sunday on it, trying to do what you said. So i found on the net
a function that i joined to the class Utility to make an array of integer.
i put a textbox and a button on a form to test. But there is some problems
due to type conversion or something like that if i understood the alert in
vb2005 express debogger....
the function to generate random seed of integers works fine, your function
too but after spending time to making them working togather :
I give up fot tonight !
my knoledge in vb is not enough efficient to find the good way... how to
put the number of one array built with : Utility.RandomNumbers(6, 1, 6)
into your function ?
i think it's a problem of conversion solved by the use of ctype ? But how
?

I need a little help more please.

ps : it's not a job class, it's me the teacher..... for pupils under 10. I
try to improve old stuff made in vb6 that you can see here
http://www.scalpa.info/decomposer.php to make them more efficient in
maths..

THANKS for ALL

Nov 12 '06 #4

P: n/a
Hello Tom

A) I will have a look to that code next week end.... some pieces of the code
resist my comprehension. And i would like to add another array of each
factor (4*10), (5*100), (2*1) and shuffle it like this : so the order of
factorization will not always be from high to low value

for example, why sometimes i've got an extra + ?
7410200
(7410200 * 1)
(74102 * 100) +
using this code in the click event of a button several times:
x = Utility.RndGen.Next(999999, 9999999)

TextBox1.Text += x.ToString ' (Utility.Factorize(x, Utility.FactorSet(0)))
doesn't work to show just the number

TextBox1.Text += vbCrLf

TextBox1.Text += (Utility.Factorize(x, Utility.FactorSet(3)))

TextBox1.Text += vbCrLf

TextBox1.Text += (Utility.Factorize(x,
Utility.Shuffle(Utility.FactorSet(3))))
B) I made my home work and change integer to long at 3 places :
Public Shared Function Factorize(ByVal num As Long, ByVal order As
Integer(), optional byval melange as boolean= false) As String

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) = CInt(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

Result += "(" + res(i).ToString + " * " + div(i).ToString + ")" _

+ IIf(i 0, " + ", "").ToString

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

Next

Return Result

End Function

C) I wonder if i have to change it in the shuffle function : we use the
"word" : order byref, also here, so ?
it seems to work without any changes.

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 !

take care
pascal
Nov 15 '06 #5

P: n/a
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


Nov 16 '06 #6

P: n/a
I worked around the melange and shuffle methods...and add the factorization
for decimal numbers and i've got this kind of answers :

Entier seul non mélangé: (12 * 1000) + (3 * 100) + (4 * 10)
Entier seul mélangé: (3 * 100) + (12 * 1000) + (4 * 10)
Entier seul shuffle et mélangé: (12 * 1000) + (34 * 10)
Entier seul shuffle et non mélangé: (123 * 100) + (4 * 10)

Décimal seul non mélangé: (123 * 100) + (4 * 10) + (56 * 0.01) + (7 * 0.001)
+ (8 * 0.0001)
Décimal seul mélangé: (7 * 0.001) + (56 * 0.01) + (8 * 0.0001)
Décimal seul shuffle et mélangé: (5 * 0.1) + (678 * 0.0001)
Décimal seul shuffle et non mélangé: (123 * 100) + (4 * 10) + (56 * 0.01) +
(7 * 0.001) + (8 * 0.0001)
with a code like this :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim array4 As Integer() = {3, 2, 1, 0}

Dim nb As Long() = {12340, 5678} 'before it was num an integer, now it's an
array of integers first part = integer part, second part is the decimal
part(never exceeds 3 characters now for my pupils) of the number...

TextBox1.Text = ""
TextBox1.Text += ("Entier seul non mélangé: " + Utility.FactorDeci(nb,
array4, True, False)) + vbCrLf

TextBox1.Text += ("Entier seul mélangé: " + Utility.FactorDeci(nb, array4,
True, True)) + vbCrLf

TextBox1.Text += ("Entier seul shuffle et mélangé: " +
Utility.FactorDeci(nb, Utility.Shuffle(array4), True, True)) + vbCrLf

TextBox1.Text += ("Entier seul shuffle et non mélangé: " +
Utility.FactorDeci(nb, Utility.Shuffle(array4), True, False)) + vbCrLf +
vbCrLf

TextBox1.Text += ("Décimal seul non mélangé: " + Utility.FactorDeci(nb,
array4, False, False)) + vbCrLf

TextBox1.Text += ("Décimal seul mélangé: " + Utility.FactorDeci(nb, array4,
False, True)) + vbCrLf

TextBox1.Text += ("Décimal seul shuffle et mélangé: " +
Utility.FactorDeci(nb, Utility.Shuffle(array4), False, True)) + vbCrLf

TextBox1.Text += ("Décimal seul shuffle et non mélangé: " +
Utility.FactorDeci(nb, Utility.Shuffle(array4), False, False)) + vbCrLf

End Sub

Now the trick is to build the GenArray with the length of each part of the
number (I think that this array does not need to be larger than the quantity
of characters of the number. ? No)
i think it has to become a two dimensionals Array because i have to store
informations for the integer part and the decimal part. And the lenght of
each dimension must not exceed the lenght of each part. example :
for the number 12345.6789
12345 is the integer part and it has a lenght of 5 .
6789 is the decimal part and it has a length of 4.
so if i want to factorize this number in a random way i have to put theese
variables in the GenArray like this : GenArray(4,3) the first dimension
will contain something like this {0, 1, 2, 3} and the second dimension
something like that : {0, 1, 2} and shuffle it if needed. So the shuffle
function has to become a two dimensions array also ?

I don't know how to use the len(PartEntiere) in the genarray ? Is it
possible to write
something like this :
TextBox1.Text = Utility.FactorDeci(Utility.GenNombre(100, 1000),
Utility.Shuffle(Utility.GenArray(tailleEntiere,tai lleDecimale), False,
False) + vbCrLf
or like this :
TextBox1.Text = Utility.FactorDeci(Utility.GenNombre(100, 1000),
Utility.Shuffle(Utility.GenArray(TabNbre(2),TabNbr e(3)), False, False) +
vbCrLf

where tailleEntiere and tailleDecimale come from GenNombre function ? (

that means the function factorDeci can generate a random decimal number,
tests the length of each part and uses these lengths to build the Array .
I don't know the way to get those numbers stored in the function (I need
help here)
here is GenNombre :
Public Shared Function GenNombre(ByVal low As Long, ByVal high As Long) As
Long()

'crée un nombre décimal aléatoire avec au plus 3 chiffres après la virgule,

' entre min et max et ajoute 1 pour que la borne supérieure soit comprise.

Randomize()

Dim PartieEntiere As Long = 0

Dim PartieDecimale As Long = 0

Dim NbreTire As Double = 0

Do

NbreTire = Math.Round(RndGen.Next(low, high) + RndGen.NextDouble(), 3)

Loop Until NbreTire 0

PartieEntiere = Int(NbreTire)

PartieDecimale = CLng(Mid$(CStr(NbreTire), InStr(CStr(NbreTire) & ".", ".")
+ 1))

Dim tailleEntiere As Integer = Len(PartieEntiere)

Dim tailleDecimale As Integer = Len(PartieDecimale)

Dim TabNbre(1) As Long 'stocker le nombre en deux morceaux dans un tableau

TabNbre(0) = PartieEntiere

TabNbre(1) = PartieDecimale

TabNbre(2) = tailleEntiere 'stocker la taille de chaque partie

TabNbre(3) = tailleDecimale

Return TabNbre

End Function

################################################## ##############################

Public Class Utility

Inherits Object

Implements System.Collections.IComparer

' 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

#Region "Tom Leylan"

Public Shared Function Factorize(ByVal num As Long, ByVal order As
Integer(), Optional ByVal melanger As Boolean = False) 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 Montableau(order.Length - 1) As String 'pascal

Dim Result As String = ""

Dim resEnt(order.Length - 1) As Long

order.CopyTo(resEnt, 0)

Dim divEnt(order.Length - 1) As Long

order.CopyTo(divEnt, 0)

Dim bal As Long = num

For i As Integer = 0 To (order.Length - 1)

Dim val As Integer = order(i)

divEnt(val) = CLng(10 ^ val)

resEnt(val) = (bal \ divEnt(val))

bal -= (divEnt(val) * resEnt(val))

Next

For i As Integer = (order.Length - 1) To 0 Step -1

If (resEnt(i) 0) Then

If (Result.Length 0) Then Result += " + "

Result += "(" + resEnt(i).ToString + " * " + divEnt(i).ToString + ")"

Montableau(i) = "(" + resEnt(i).ToString + " * " + divEnt(i).ToString + ")"
'pascal

End If

Next

'mélanger la proposition si nécessaire

If melanger = True Then

Result = ""

Array.Sort(Montableau)

For i As Integer = (Montableau.Length - 1) To 0 Step -1

If (Montableau(i) <"") Then Result += Montableau(i) + " + "

Next

Result = Mid$(Result, 1, Len(Result) - 3)

End If

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 Region

#Region "Pascal"

Public Shared Function melange(ByVal MonTAb As String()) As String()

Dim rnd As Integer

Dim val As String

Dim max As Integer = (MonTAb.Length - 1)

For i As Integer = 0 To max

val = MonTAb(i)

rnd = RndGen.Next(0, max + 1)

MonTAb(i) = MonTAb(rnd)

MonTAb(rnd) = val

Next

Return MonTAb

End Function

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare

If Object.Equals(x, y) Then

Return 0

Else

Return RndGen.Next(-1, 1)

End If

End Function

Public Shared Function GenNombre(ByVal low As Long, ByVal high As Long) As
Long()

'crée un nombre décimal aléatoire avec au plus 3 chiffres après la virgule,

' entre min et max et ajoute 1 pour que la borne supérieure soit comprise.

Randomize()

Dim PartieEntiere As Long = 0

Dim PartieDecimale As Long = 0

Dim NbreTire As Double = 0

Do

NbreTire = Math.Round(RndGen.Next(low, high) + RndGen.NextDouble(), 3)

Loop Until NbreTire 0

PartieEntiere = Int(NbreTire)

PartieDecimale = CLng(Mid$(CStr(NbreTire), InStr(CStr(NbreTire) & ".", ".")
+ 1))

Dim tailleEntiere As Integer = Len(PartieEntiere)

Dim tailleDecimale As Integer = Len(PartieDecimale)

Dim TabNbre(1) As Long 'stocker le nombre en deux morceaux dans un tableau

TabNbre(0) = PartieEntiere

TabNbre(1) = PartieDecimale

TabNbre(2) = tailleEntiere 'stocker la taille de chaque partie

TabNbre(3) = tailleDecimale

Return TabNbre

End Function

Public Shared Function FactorDeci(ByVal TabNbre As Long(), ByVal order As
Integer(), Optional ByVal EntierSeul As Boolean = True, Optional ByVal
melanger As Boolean = False) 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 LeNbre(1) As Long 'stocker le nombre aléatoire dans un tableau de deux
éléments

TabNbre.CopyTo(LeNbre, 0)

Dim Montableau(order.Length - 1) As String 'déclaration d'un tableau qui
servira de stockage pour le mélange éventuel

Dim Result As String = ""

'On traite la partie entière

Dim resEnt(order.Length - 1) As Long

order.CopyTo(resEnt, 0)

Dim divEnt(order.Length - 1) As Long

order.CopyTo(divEnt, 0)

Dim balEnt As Long = LeNbre(0) 'le premier élément est la partie entière

For i As Integer = 0 To (order.Length - 1) 'on factorise en fonction du
tableau "order"

Dim val As Integer = order(i)

divEnt(val) = CLng(10 ^ val)

resEnt(val) = (balEnt \ divEnt(val))

balEnt -= (divEnt(val) * resEnt(val))

Next

For i As Integer = (order.Length - 1) To 0 Step -1

If (resEnt(i) 0) Then

If (Result.Length 0) Then Result += " + "

Result += "(" + resEnt(i).ToString + " * " + divEnt(i).ToString + ")" 'on
stocke pour affichage

Montableau(i) = "(" + resEnt(i).ToString + " * " + divEnt(i).ToString + ")"
'on stocke pour mélanger

End If

Next

'On traite la partie décimale

If EntierSeul = False Then

Dim resDeci(order.Length - 1) As Integer

order.CopyTo(resDeci, 0)

Dim div(order.Length - 1) As Single

order.CopyTo(div, 0)

Dim balDeci As Integer = LeNbre(1) 'le deuxième élément est la partie
décimale

For i As Integer = 0 To (order.Length - 1) 'on factorise en fonction du
tableau "order"

Dim val As Integer = order(i)

div(val) = (10 ^ -(order.Length - val))

resDeci(val) = (Int(balDeci \ (10 ^ val)))

balDeci -= ((10 ^ val) * resDeci(val))

Next

For i As Integer = (order.Length - 1) To 0 Step -1

If (resDeci(i) 0) Then

If (Result.Length 0) Then Result += " + "

Result += "(" + resDeci(i).ToString + " * " + div(i).ToString + ")" 'on
stocke pour affichage

Montableau(i) = "(" + resDeci(i).ToString + " * " + div(i).ToString + ")"
'on stocke pour mélanger

End If

Next

End If ' fin EntierSeul

'mélanger la proposition si nécessaire

If melanger = True Then

Result = "" 'on vide le résultat...

'Array.Sort(Montableau)

melange(Montableau)

For i As Integer = (Montableau.Length - 1) To 0 Step -1

If (Montableau(i) <"") Then Result += Montableau(i) + " + " '...et on le
remplace par le contenu du tableau mélangé

Next

Result = Mid$(Result, 1, Len(Result) - 3) 'On enlève le dernier +

End If

Return Result

End Function

#End Region

End Class

################################################## ##############################

I hope you will understand,
thanks
pascal
Nov 22 '06 #7

P: n/a
oops a mistake here is the change.. i need a redim preserve when i want
decimal numbers...
ReDim Preserve Montableau(z + order.Length - 1)
'On traite la partie décimale

Dim z As Integer = Montableau.Length

If EntierSeul = False Then

Dim resDeci(order.Length - 1) As Integer

order.CopyTo(resDeci, 0)

Dim div(order.Length - 1) As Single

order.CopyTo(div, 0)

Dim balDeci As Integer = LeNbre(1) 'le deuxième élément est la partie
décimale

For i As Integer = 0 To (order.Length - 1) 'on factorise en fonction du
tableau "order"

Dim val As Integer = order(i)

div(val) = (10 ^ -(order.Length - val))

resDeci(val) = (Int(balDeci \ (10 ^ val)))

balDeci -= ((10 ^ val) * resDeci(val))

Next

For i As Integer = (order.Length - 1) To 0 Step -1

If (resDeci(i) 0) Then

If (Result.Length 0) Then Result += " + "

Result += "(" + resDeci(i).ToString + " * " + div(i).ToString + ")" 'on
stocke pour affichage

ReDim Preserve Montableau(z + order.Length - 1)

Montableau(i + z - 1) = "(" + resDeci(i).ToString + " * " + div(i).ToString
+ ")" 'on stocke pour mélanger

End If

Next

End If ' fin EntierSeul
Nov 22 '06 #8

P: n/a
I'd like to be of help but you lost me on all this. What's the point of all
the new stuff?

It looks like you are trying to introduce the ability to handle decimal
numbers which off the top of my head doesn't seem to need too much special
handling (though I haven't written it.) Aren't they handled with negative
powers of 10? In any case I wouldn't start by introducing a two-part
decimal value but rather I would declare and pass a Double and let the
Factorize() method handle the grunt work (or pass the task along to another
method) to do it.

It looks like you've gone a bit overboard on the ".Net isms" as well. What
is the point of implementing Systems.Collections.IComparer, is there a
collection here somewhere? You can compare things without implementing
IComparer.

The best advice (I think) I can give is "keep it simple"... the moment you
start adding Array.Sort, Mid$() and optional parameters to the mix you need
to ask yourself if you've gone too far. Perhaps you haven't (everything is
useful somewhere) but ask yourself the question is "is everything needed
here." Again I don't exactly get what you're trying to do but you have to
make sure you don't turn a "machine" into a "can of worms." If you can't
explain what it is doing you've done it wrong... keep it simple!

Tom
"Pascal" <sc*********@wanadoo.rfwrote in message
news:45***********************@news.orange.fr...
oops a mistake here is the change.. i need a redim preserve when i want
decimal numbers...
ReDim Preserve Montableau(z + order.Length - 1)
'On traite la partie décimale

Dim z As Integer = Montableau.Length

If EntierSeul = False Then

Dim resDeci(order.Length - 1) As Integer

order.CopyTo(resDeci, 0)

Dim div(order.Length - 1) As Single

order.CopyTo(div, 0)

Dim balDeci As Integer = LeNbre(1) 'le deuxième élément est la partie
décimale

For i As Integer = 0 To (order.Length - 1) 'on factorise en fonction du
tableau "order"

Dim val As Integer = order(i)

div(val) = (10 ^ -(order.Length - val))

resDeci(val) = (Int(balDeci \ (10 ^ val)))

balDeci -= ((10 ^ val) * resDeci(val))

Next

For i As Integer = (order.Length - 1) To 0 Step -1

If (resDeci(i) 0) Then

If (Result.Length 0) Then Result += " + "

Result += "(" + resDeci(i).ToString + " * " + div(i).ToString + ")" 'on
stocke pour affichage

ReDim Preserve Montableau(z + order.Length - 1)

Montableau(i + z - 1) = "(" + resDeci(i).ToString + " * " +
div(i).ToString + ")" 'on stocke pour mélanger

End If

Next

End If ' fin EntierSeul


Nov 22 '06 #9

P: n/a
hello Tom !

first of all, thanks for spending time in reading the previous messy post.
Here is the goal for me with this stuff:
I'd like to improve the skill of my pupils in reading integer and decimal
numbers.
Among all the steps used to build this skill :
One of the steps to reach this goal is to make them able to read
factorization of these kind of numbers
and thus find the number itself.
That's why I need several adjustments, to built difficult or easy exercices
according to the abilities of the child.

example : from easy to very hard
Entier seul non mélangé: (1 * 1000) + (2 * 100) + (3 * 10) + (4 * 1)
Entier seul mélangé: (3 * 10) + (4 * 1) + (1 * 1000) + (2 * 100) the child
needs to re-order the factors before finding a solution. So it's more
difficult.
Entier seul shuffle et non mélangé: (12 * 100) + (3 * 10) + (4 * 1)
Entier seul shuffle et mélangé: (34 * 1) + (2 * 100) + (1 * 1000) the
factors are not always the same, so i increase the difficulties.

if i factorize decimal numbers with a decimal part not too long : 3 digits
after the point only... I increase one more time the difficulty.
Décimal non mélangé: (1234 * 1) + (5678 * 0.0001)
Décimal mélangé: (1234 * 1) + (5678 * 0.0001)
Décimal shuffle et non mélangé: (123 * 10) + (4 * 1) + (567 * 0.001) + (8 *
0.0001)
Décimal shuffle et mélangé: (12 * 100) + (56 * 0.01) + (78 * 0.0001) + (34 *
1)

So the software i would like to build looks like this one i did before
http://www.scalpa.net/decomposer.php and I want to improve it by adding
these adjustements and the possibility for me to print a sheet of this kind
of exercices taking account of these adjustements. All that in one shot !

################# I need help here###########################
So to make the soft able to do that : I think some improvements are missing
:
first : when i call the function : TextBox1.Text += ( Utility.Factorize(nb,
array4, True, False)) it works well.
but It could be better if i can build the array according of the lenght of
the number. I believe that the max of this array is equal of the lenght of
the integer part or the decimal part. So if i want to automate the creation
of the sheet of exercices, it's necessary to find a solution in this way.
I tried to find a solution by storing the lenghts of the integeer and of the
decimal part in the same array of Function GenNombre:
TabNbre(2) = tailleEntiere 'storing the length of each part to use it in
genArray
TabNbre(3) = tailleDecimale
but i was'nt able to get them in the code of form1

Thus : how to recover these informations in genArray from the form1 ?
Utility.Factorize(nb, Utility.GenArray(???), True, False))

i think (???) must be an array built with TabNbre(2) and TabNbre(3) . It's
here where i am lost....

################################################## #####
Now for the modification of the code :
the purpose of melange method is to mix the array so Children is not always
in front of the same order 10^3*10^2*10^1*10^0 like this :
(1 * 1000) + (2 * 100) + (3 * 10) + (4 * 1) but he can have to read also
this : (3 * 10) + (2 * 100) +(4 * 1) + (1 * 1000) that is more difficult .

So I tried to make it simple and use the utility.factorize function with
some variations as you can see under : I though about using a double for
decimal number but building an array of its factorisation with positive an
negative power of ten, and taking account of the decimal point, did'nt
appear so simple....
It works like that, and i tried to keep it simple ! As you said.

"It looks like you've gone a bit overboard on the ".Net isms" as well. What
is the point of implementing Systems.Collections.IComparer, is there a
collection here somewhere? You can compare things without implementing
IComparer." =I let some pieces of code ..... No needs of it for sure
......Oops..

I add some informations in english and modified some others from yours :
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 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

Public Shared Function melange(ByVal MonTAb As String()) As String()

'i had this here to have the hability to increase the skill of my pupils in
reading and calculating such factorization

'without that, the factors are always arranged from larger to smaller like
this :

'123456,7689 could be returned as: (123 * 1000) + (456 * 1) + (76 * 0.01)
+( 89 * 0.0001)

'and with that melange we can have : ( 689 * 0.0001) + (6 * 1) + (5 * 10) +
(7 * 0.1) + (1234 * 100)

Dim rnd As Integer

Dim val As String

Dim max As Integer = (MonTAb.Length - 1)

For i As Integer = 0 To max

val = MonTAb(i)

rnd = RndGen.Next(0, max + 1)

MonTAb(i) = MonTAb(rnd)

MonTAb(rnd) = val

Next

Return MonTAb

End Function

Public Shared Function GenNombre(ByVal low As Long, ByVal high As Long) As
Long()

'creates a random decimal number with never more than 3 digits after the
comma

'between low and high and adds 1 so that the upper limit is included.

'split this number in two pieces to handle both the positive and the
negative factorization later because that is not the same "treatment" for
each part

Randomize()

Dim PartieEntiere As Long = 0

Dim PartieDecimale As Long = 0

Dim NbreTire As Double = 0

Do

NbreTire = Math.Round(RndGen.Next(low, high) + RndGen.NextDouble(), 3)

Loop Until NbreTire 0

PartieEntiere = Int(NbreTire)

PartieDecimale = CLng(Mid$(CStr(NbreTire), InStr(CStr(NbreTire) & ".", ".")
+ 1))

Dim tailleEntiere As Integer = Len(PartieEntiere)

Dim tailleDecimale As Integer = Len(PartieDecimale)

Dim TabNbre(1) As Long 'store the two pieces in an array

TabNbre(0) = PartieEntiere

TabNbre(1) = PartieDecimale

Return TabNbre

End Function

Public Shared Function Factorize(ByVal TabNbre As Long(), ByVal order As
Integer(), Optional ByVal EntierSeul As Boolean = True, Optional ByVal
melanger As Boolean = False) As String

' Factorize returns a string that represents the value passed as TabNbre()
in the form of an

' equation made up of factors of 10 positive or negative

' 123456,7689 could be returned as: (123 * 1000) + (456 * 1) + (76 * 0.01)
+( 89 * 0.0001)

' or 123456 could be returned as: (1234 * 100) + (5 * 10) + (6 * 1) + (7 *
0.1) +( 689 * 0.0001)

' the format is controlled by an integer array named order

Dim LeNbre(1) As Long 'keep the random number in an array of two elements

TabNbre.CopyTo(LeNbre, 0)

Dim Montableau(order.Length - 1) As String 'declaration of an array which
will be used as storage for the possible mixture ("melange")

Dim Result As String = ""

'The integer part is treated

Dim resEnt(order.Length - 1) As Long

order.CopyTo(resEnt, 0)

Dim divEnt(order.Length - 1) As Long

order.CopyTo(divEnt, 0)

Dim balEnt As Long = LeNbre(0) 'the first element is the integer part

For i As Integer = 0 To (order.Length - 1) 'one factorizes with positive
power of ten according to table "order"

Dim val As Integer = order(i)

divEnt(val) = CLng(10 ^ val)

resEnt(val) = (balEnt \ divEnt(val))

balEnt -= (divEnt(val) * resEnt(val))

Next

For i As Integer = (order.Length - 1) To 0 Step -1

If (resEnt(i) 0) Then

If (Result.Length 0) Then Result += " + "

Result += "(" + resEnt(i).ToString + " * " + divEnt(i).ToString + ")" 'one
stores for printing on screen

Montableau(i) = "(" + resEnt(i).ToString + " * " + divEnt(i).ToString + ")"
'one stores for "melanging"

End If

Next

'The decimal part is treated if needed...

'I keep the lengh of this array to redim preserve later to keep the
information of the integer part

Dim z As Integer = Montableau.Length

If EntierSeul = False Then

Dim resDeci(order.Length - 1) As Integer

order.CopyTo(resDeci, 0)

Dim div(order.Length - 1) As Single

order.CopyTo(div, 0)

Dim balDeci As Integer = LeNbre(1) 'the second element is the decimal part

For i As Integer = 0 To (order.Length - 1) 'one factorizes with positive
power of ten according to table "order"

Dim val As Integer = order(i)

div(val) = (10 ^ -(order.Length - val)) 'negative power of ten this time

resDeci(val) = (Int(balDeci \ (10 ^ val)))

balDeci -= ((10 ^ val) * resDeci(val))

Next

For i As Integer = (order.Length - 1) To 0 Step -1

If (resDeci(i) 0) Then

If (Result.Length 0) Then Result += " + "

Result += "(" + resDeci(i).ToString + " * " + div(i).ToString + ")" 'i store
the result for showing on screen

ReDim Preserve Montableau(z + order.Length - 1)

Montableau(i + z - 1) = "(" + resDeci(i).ToString + " * " + div(i).ToString
+ ")" 'i store the result for mixing before showing on screen

End If

Next

End If ' fin EntierSeul

'Lastly, mix the result if necessary.

If melanger = True Then

Result = "" 'the result is emptied.

melange(Montableau) ' I mix

For i As Integer = (Montableau.Length - 1) To 0 Step -1

If (Montableau(i) <"") Then Result += Montableau(i) + " + " 'and one
replaces it by the contents of the mixed array

Next

Result = Mid$(Result, 1, Len(Result) - 3) 'I remove the last +, added here
because of the loop, but don't needed

End If

Return Result

End Function

End Class

I will post this from school.
I hope it's clearer.
pascal
Nov 24 '06 #10

P: n/a
"scalpa" <scalpaNoSpam@wanadooDotfrwrote...
Here is the goal for me with this stuff:
Interesting...
first : when i call the function : TextBox1.Text += (
Utility.Factorize(nb, array4, True, False)) it works well.
A couple of points. First try to avoid unadorned boolean parameters. These
are settings of some sort but True and False tells us nothing. Add enum
values to the Utility class which represent the various settings. If the
"False" is (for example) "Mix" then it is clearer if something like
Utility.Mix and Utility.NoMix can represent that parameter. Then people
reading it (including us a year from now) don't have to remember what the
4rd parameter does and why we passed False to it.
but It could be better if i can build the array according of the lenght of
the number. I believe that the max of this array is equal of the lenght
of the integer part or the decimal part.
The size of the array is somewhat related but as far as I recall it doesn't
matter what size the array is. If you send the method an array with one
element it will probably return 1 * number, if you pass a two element array
it has more permutations and on up. Clearly if you tell it to factorize the
10,000's position but the number is 5,650 there won't be anything in it but
it doesn't return any text related to the 10,000's position so it isn't a
problem.
Now for the modification of the code :
the purpose of melange method is to mix the array so Children is not
always in front of the same order 10^3*10^2*10^1*10^0 like this :
(1 * 1000) + (2 * 100) + (3 * 10) + (4 * 1) but he can have to read also
this : (3 * 10) + (2 * 100) +(4 * 1) + (1 * 1000) that is more difficult
.
Ah, I see. In that case you should find a way to create an array of
elements you can send to the Shuffle method, remember it will shuffle any
integer array. So send it an array representing the elements of the order
array. Instead of looping through in "order" order you would shuffle an
array of { 1, 2, 3, 4 ,5, 6 } (of whatever length you need) and use the
shuffled result to decide which element of res(i) and div(i) to access next.
Again there is no need for random numbers except the need of the shuffle
algorithm. You shouldn't add code to test whether some random number has
been chosen and then chose another. There is a finite set of values we just
shuffle them.

In fact (I'm browsing the code) and I think you can probably just use the
order array as the order to collect the strings. It is already shuffled and
contains numeric values of increasing size so let them be used as the array
index when building the string.
Public Shared Function GenNombre(ByVal low As Long, ByVal high As Long) As
Long()
Randomize()
You never need to call Randomize(), the RndGen object has handled it already
and it isn't likely to listen to Randomize() in any case.
NbreTire = Math.Round(RndGen.Next(low, high) + RndGen.NextDouble(), 3)

Loop Until NbreTire 0

PartieEntiere = Int(NbreTire)

PartieDecimale = CLng(Mid$(CStr(NbreTire), InStr(CStr(NbreTire) & ".",
".") + 1))
Don't quite get the reasoning behind this part. If you need the number in
two "parts" why go to the trouble of adding them together? If you need
PartieEntiere and PartieDecimale then it might be better to generate each of
them. This will eliminate your CLng(Mid$(CStr...)) equation. If you really
want to split the value into two parts however I'd suggest you take
advantage of the Utility class you have and add another method,
SplitDouble(). Either use a parameter to select which part you want back or
pass two variables (by reference) that it will set to the values of each
part. Again (don't mean to sound like a broken record) you end up with a
method you can use in other situations. GenNombre() will use it but other
routines (anywhere) can also.
Public Shared Function Factorize(ByVal TabNbre As Long(), ByVal order As
Integer(), Optional ByVal EntierSeul As Boolean = True, Optional ByVal
melanger As Boolean = False) As String
It is much too complicated for me to read though this code. Personally I'd
avoid the extra settings and optional parameters. As I mentioned before I
think you can extend Factorize() to handle negative powers of 10. It is
possible that the results aren't quite what you want since it may return
things like (494949 * .001) at some point. If that is acceptable then I
believe it will work.
ReDim Preserve Montableau(z + order.Length - 1)

Montableau(i + z - 1) = "(" + resDeci(i).ToString + " * " +
div(i).ToString + ")" 'i store the result for mixing before showing on
screen

End If
Certain constructs in code jump out at me and ReDim(ming) an array is one of
them. Not that it is wrong but I never do it so it leads me to believe
something is up with a design that requires it.
I will post this from school.
I hope it's clearer.
pascal
A bit :-) You will get it to work I'm certain. One thing I do is to
picture the process long before I write the code. Draw the arrays on paper
with a pen and figure out the actions that are needed to manipulate it.
Then create methods to perform those actions. If you write separate
Utility.<methodmethods they can be tested out independently of the final
goal. Each part must work before you combine them into a bigger process.
If something doesn't work you can pinpoint if it is the Melanger or the
Factorizer or something else. If instead you write a few large "do it all"
methods and something is wrong you have to hunt through the entire "do it
all" method and that's a pain.

Tom


Nov 24 '06 #11

P: n/a


--
bonjour Tom

I did it ..... ;0} with your help... and now i am just working around
printing the stuff.
Soon Xmas, so let me know, if you like, an adress where to send the gift of
compensation. wages of sweat
you can find my email adress here :
http://www.scalpa.info

merry Xmas
pascal
Dec 15 '06 #12

P: n/a
I'm glad to hear that you persevered... I knew you could do it. Frankly the
important thing (in my mind) is to actually work through the process. If
somebody tells you something the lesson almost never sinks in and surely not
as well as when you're working at it 14 hours straight, it's 2 a.m. and the
project is due tomorrow morning :-)

"Pascal" <sc*********@wanadoo.rfwrote in message
news:45***********************@news.orange.fr...
--
bonjour Tom

I did it ..... ;0} with your help... and now i am just working around
printing the stuff.
Soon Xmas, so let me know, if you like, an adress where to send the gift
of compensation. wages of sweat
you can find my email adress here :
http://www.scalpa.info

merry Xmas
pascal

Dec 16 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.