Hi! I can't reproduce your problem. Randomize with seed 12345, I got; >From 0 to 9: 4 0 7 9 5 1 2 6 3 8 >From 9 to 0: 4 2 7 8 3 0 6 9 1 5 Both correct.
So maybe there is a bug in SWAP command with Gambas version... what are you using? With Gambas 2.10 it is working. Jussi P.S. Code I used to test: DIM ii AS Integer DIM jj AS Integer DIM a AS Integer DIM myArray AS Integer[200] DIM checkArray AS NEW Integer[] RANDOMIZE 12345 FOR ii = 0 TO 9 '199 myArray[ii] = ii NEXT FOR ii = 0 TO 9 '199 ' random swap array items a = Int(Rnd(ii + 1)) SWAP myArray[ii], myArray[a] NEXT FOR ii = 0 TO 9 '199 ' Check against duplicates. PRINT myArray[ii] FOR jj = 0 TO 9 '199 IF myArray[ii] = jj THEN IF checkArray.Exist(jj) = FALSE THEN checkArray.Add(jj) ELSE Message.Error("Error!") RETURN ENDIF ENDIF NEXT NEXT PRINT checkArray.Count On Wed, Apr 8, 2009 at 00:12, Simonart Dominique <simonart.domini...@wanadoo.fr> wrote: > Hi, > > jbskaggs a écrit : >> Okay, >> >> But why does it do that? I don't see why the direction should matter what >> am I missing ? Becuase maybe this is something that has plagued me and >> caused me headaches for a while. >> >> JB Skaggs >> > > You're right! Thanks! > My mistake is that I listed the myArray values inside the > SAME for next loop that the swap! But of course, the values > could change at each next step > > However, there is differences. Let see a step by step sample > > CASE 1: Forward FOR NEXT loop > > * i=0 -> Int(Rnd(i+1)) = 0! > you could affect only myArray[0] to myArray[0] > * i=1 -> Int(Rnd(i+1)) = 0-1 > you could affect myArray[0] once more > * i=2 -> Inn(Rnd(i+1)) = 0-2 > idem > * i=3 ... > at each step, ALWAYS you could affect an already selected value > > CASE 2: Backward FOR NEXT loop > > * i=199 -> Int(Rnd(i+1)) = 0-199 > myArray[199] could be any value > * i=198 -> Int(Rnd(i+1)) = 0-198 > myArray[198] could be any of the non selected values > myArray[199] will never be affected anymore > * i=197 ... > at each step, NEVER you could affect an already selected value > > I found the second case more satisfying for my mind! :) > > Hope this send your headache away! :) > Dominique Simonart >> >> >> >> Simonart Dominique wrote: >>> Hi, >>> >>> jbskaggs a écrit : >>>> When I ran the code it ran fine- why is it important to have it count >>>> backwards in the random swap? >>>> >>>> ie for i =199 to 0 step -1 instead of for i=0 to 199 step 1? >>>> >>>> JB SKaggs >>>> >>> Strange, because it should not :) >>> run a test with 10 numbers 0-9 and fixe the seed with >>> RANDOMIZE 12345 >>> If you run the forward FOR NEXT you will get: >>> 0 0 2 3 1 1 2 6 3 8 >>> which is wrong! >>> but if you run the backward FOR NEXT you will get: >>> 5 1 9 6 0 3 8 7 2 4 >>> which is right! >>> >>> May be you did not notice it because 200 is long enough >>> before you get the same number twice, and before you remark >>> that some are missing? >>> >>> Dominique Simonart >>> >>>> Simonart Dominique wrote: >>>>> Hi, >>>>> >>>>> Hmm, did you run exactly this code? If so, there is >>>>> something wrong with the second FOR NEXT loop >>>>> >>>>> jbskaggs a écrit : >>>>>> using your suggestions and the swap command vs manual swapping this is >>>>>> my >>>>>> code: >>>>>> >>>>>> public b as string 'optional string used to hold the array data for >>>>>> file >>>>>> or >>>>>> split later >>>>>> >>>>>> PUBLIC SUB button4_click() ' initialize array >>>>>> DIM myArray AS Integer[200] >>>>>> DIM a AS Integer >>>>>> DIM i AS Integer >>>>>> >>>>>> FOR i = 0 TO 199 STEP 1 'add array items >>>>>> myArray[i] = i >>>>>> NEXT >>>>>> >>>>>> FOR i = 0 TO 199 STEP 1 ' random swap array items >>>>>> a = Int(Rnd(i + 1)) >>>>>> SWAP myArray[i], myArray[a] >>>>>> NEXT >>>>>> >>>>> the right code is: >>>>> >>>>> FOR i = 199 TO 0 STEP -1 >>>>> ... >>>>> NEXT >>>>> >>>>>> FOR i = 0 TO 199 STEP 1 ' write items in listview2 >>>>>> listview1.MoveTo(myArray[i]) >>>>>> c = listview1.Item.Key >>>>>> listview2.add(c, listview1.item.text) >>>>>> NEXT >>>>>> >>>>>> >>>>>> >>>>>> FOR i = 0 TO 199 STEP 1 'optional step to write array as a string for >>>>>> file >>>>>> or whatever use >>>>>> b &= "slot" & myArray[i] & "," >>>>>> NEXT >>>>>> PRINT b >>>>>> END >>>>>> >>>>>> PUBLIC SUB Button1_Click() 'an optional way to load the array values >>>>>> from >>>>>> the variable b and / or a file if b was file loaded >>>>>> DIM egg AS String[] >>>>>> DIM c AS String >>>>>> listview1.Clear >>>>>> egg = Split(b, ",") >>>>>> FOR EACH c IN egg >>>>>> TRY listview1.Add(c, c) >>>>>> IF ERROR THEN RETURN >>>>>> NEXT >>>>>> IF listview1.Count >= 200 THEN RETURN >>>>>> END >>>>>> >>>>>> JB Skaggs >>>>>> >>>>> Dominique Simonart >>>>> >>> Dominique Simonart >>> >>> >>> ------------------------------------------------------------------------------ >>> This SF.net email is sponsored by: >>> High Quality Requirements in a Collaborative Environment. >>> Download a free trial of Rational Requirements Composer Now! >>> http://p.sf.net/sfu/www-ibm-com >>> _______________________________________________ >>> Gambas-user mailing list >>> Gambas-user@lists.sourceforge.net >>> https://lists.sourceforge.net/lists/listinfo/gambas-user >>> >>> >> > > > > ------------------------------------------------------------------------------ > This SF.net email is sponsored by: > High Quality Requirements in a Collaborative Environment. > Download a free trial of Rational Requirements Composer Now! > http://p.sf.net/sfu/www-ibm-com > _______________________________________________ > Gambas-user mailing list > Gambas-user@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/gambas-user > ------------------------------------------------------------------------------ This SF.net email is sponsored by: High Quality Requirements in a Collaborative Environment. Download a free trial of Rational Requirements Composer Now! http://p.sf.net/sfu/www-ibm-com _______________________________________________ Gambas-user mailing list Gambas-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user