435,561 Members | 3,265 Online + 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
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" 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"

 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"

 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 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" 