Re: [Gambas-user] A random sort of listview
jbskaggs ha scritto: It is Gambas related! Where else are noobs like me going to learn this? Please continue as you are both teaching me Maestros! To stop now would be like taking candy from a baby just after the wrapper was opened. Thanks for your appreciation words. What Dominique said, I think, is that when the gambas part of the discussion is over, it is best to continue the discussion elsewhere, if wanted, as this list is for gambas questions. Nobody wants to take away your candies - when you have questions, just post to this lists and, when you have experiences to share, post again - everyone in this list is here to exchange knowledge and advice (about gambas). Regarding the randomize algorithm, well, I think we reached the goal but, if you still have doubts, try to post again... Cheers, Doriano -- 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
Re: [Gambas-user] A random sort of listview
Dominique SIMONART ha scritto: Doriano Blengino a écrit : jbskaggs ha scritto: FOR i = 0 TO ListView1.count - 1 STEP 1 myArray[i] = i NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 a = Int(Rnd(i + 1)) SWAP myArray[i], myArray[a] PRINT i, a NEXT Apart from the problem of missing records, there could be also a randomness problem in the algorithm... I am really not sure, but it seems that certain slots have more chances to be swapped than other - myarray[0] can be swapped COUNT times, while myarray[count-1] gets only a chance. I am not sure of what this signifies: it is true that the swap affects the values contained in slots, and not the slots themselves, but anyway there could be another method. One could also do this: for i = 0 to 1000 ' arbitrary value, large enough (100?) c1 = int(rnd(listview1.count)) c2 = int(rnd(listview1.count)) swap myarray[c1], myarray[c2] next This way, every slot gets the same chances to be swapped; for sufficient loops, it should make a good job... perhaps more random than before. And perhaps, as the algorthm is different, it could solve the problem of missing records (which I didn't understand...). Cheers, = I resend my message because it seems the text is missing?! :-\ This is not really a problem. The Randomizing process could be explained like this: 1) You align a sorted card deck in front of you on a table 2) then, from all the cards on the table you take a random card in your hand so there is a place without card on the table 3) you put the last card on the table in this hole, so the hole is now at the last place 4) you continue by returning to 2 until you got all the cards in your hand Now you have a randomized deck of cards in your hand 5) you put the first card you got in the last place on the table and continue to do that until you have no card in your hand. Now the randomized deck of cards is on the table. If you examine carefully this process, you will remark that each time you filled a hole with the last remaining card on the table, you could place the card you got just before in the last place, because it is free! You only have to remember to not take these cards already selected. So you could do the 5) between 3) and 4) and this is exactly which is done by the SWAP instruction! It is true that some places will be selected several times, but their content change each time (there is another card on these places each time). In no way you could loose a card in this process!!. The JBskaggs' problem, I think, is that, before he starts the process, there are twice 2 identical cards in his deck and Gambas does not admit that, so when Gambas encounters the duplicated cards it reject them, (but since TRY is used, you don't see the error!!) and finally, 2 cards are missing. Hope this is clearer :) I agree to everything you wrote. The first algorithm, the one you explain, is the more correct because with the minimum number of swaps you obtain a random sequence. It is equivalent to build another list based on the first, without using two lists, and you expressed it very well. Without analyzing too much, I said some slots get more chances than other, pointing out that the slots have different chances, and not the values inside the slots (which is different). Then, recalling to my mind the way a person shuffles cards by hand, I tried to express another algorithm, which in a certain way lets you to adjust the randomness: a person can shuffle very well, or not. But, thinking over, my algorithm does not fully respect this situation, because a true player shuffles cards in chunks, by taking the last part of a deck and scattering it in the middle of the remaining part... If someone wants to write a realistic card games, perhaps could consider this. Anyway, I repeat, you were right - the first algorithm is ok. Regards, -- Doriano Blengino Listen twice before you speak. This is why we have two ears, but only one mouth. -- 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
Re: [Gambas-user] A random sort of listview
Hi Doriano, I agree to everything you wrote. I thought the same about what you wrote! :) ... Then, recalling to my mind the way a person shuffles cards by hand, I tried to express another algorithm, which in a certain way lets you to adjust the randomness: a person can shuffle very well, or not. But, thinking over, my algorithm does not fully respect this situation, because a true player shuffles cards in chunks, by taking the last part of a deck and scattering it in the middle of the remaining part... If someone wants to write a realistic card games, perhaps could consider this. It could be interesting to define a sort of randomness evaluation, so we could compare several methods or evaluate the efficient limit to use. For exemple, you take 1000 in your method but may be 500 will be enough? I will take a 10 cards' deck as illustration I could think about 2 criters: 1) absolute difference between the initial and the final positions 2) absolute difference between 2 adjacent items The first criter is not fair because all the places are not equivalent! the 5th position could not exceed a 5 difference but the 0th position could be 9! So we have to consider the serie 0-9 as a ring were 0 is next to 9, so the maximum difference is 5. The same could be said about the second criter! Now, I will stop to talk about that because it's no more Gambas related :( cheers 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
Re: [Gambas-user] A random sort of listview
It is Gambas related! Where else are noobs like me going to learn this? Please continue as you are both teaching me Maestros! To stop now would be like taking candy from a baby just after the wrapper was opened. JB SKaggs Simonart Dominique wrote: Hi Doriano, I agree to everything you wrote. I thought the same about what you wrote! :) ... Then, recalling to my mind the way a person shuffles cards by hand, I tried to express another algorithm, which in a certain way lets you to adjust the randomness: a person can shuffle very well, or not. But, thinking over, my algorithm does not fully respect this situation, because a true player shuffles cards in chunks, by taking the last part of a deck and scattering it in the middle of the remaining part... If someone wants to write a realistic card games, perhaps could consider this. It could be interesting to define a sort of randomness evaluation, so we could compare several methods or evaluate the efficient limit to use. For exemple, you take 1000 in your method but may be 500 will be enough? I will take a 10 cards' deck as illustration I could think about 2 criters: 1) absolute difference between the initial and the final positions 2) absolute difference between 2 adjacent items The first criter is not fair because all the places are not equivalent! the 5th position could not exceed a 5 difference but the 0th position could be 9! So we have to consider the serie 0-9 as a ring were 0 is next to 9, so the maximum difference is 5. The same could be said about the second criter! Now, I will stop to talk about that because it's no more Gambas related :( cheers 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 -- View this message in context: http://www.nabble.com/A-random-sort-of-listview-tp22919766p23027115.html Sent from the gambas-user mailing list archive at Nabble.com. -- 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
Re: [Gambas-user] A random sort of listview
Hi yourself! Here is the problem in the code: FOR i = 0 TO ListView1.Count - 1 ' write items in listview2 TRY PRINT myArray[i] listview1.MoveTo(myArray[i]) TRY c = listview1.Item.Key TRY PRINT myArray[i], listview1.item.key , c, counter i: , i, total count: , listview1.Count TRY listview2.add(c, listview1.item.text) NEXT When the counter i reaches Listview1.Count -1 it gives an error and does not write the last record! I have compensated by writing an extra dummy record to the data file- but how do I code this properly to write the whole list? I have tried several things and they just give different errors. Thanks JB Simonart Dominique wrote: Hi, jbskaggs a écrit : Okay I thought this was worked out but its still not working- I always end up losing the last two list items. Here is the console output from my print statements: This first list is printing i, a from this command SWAP myArray[i], myArray[a] 21 21 20 13 19 0 18 13 17 11 16 4 15 9 14 12 13 4 12 2 11 6 10 2 9 4 8 0 7 5 6 1 5 3 4 3 3 0 2 0 1 0 0 0 Here, you listed i and a but the interesting values are i and myArray[i] instead This list shows where listview1 is being copied into listview2: listview1.item.key 21 counter i: 21 total count: 22 listview1.item.key 13 counter i: 20 total count: 22 listview1.item.key 13 counter i: 19 total count: 22 listview1.item.key 20 counter i: 18 total count: 22 listview1.item.key 11 counter i: 17 total count: 22 listview1.item.key 4counter i: 16 total count: 22 listview1.item.key 9counter i: 15 total count: 22 listview1.item.key 12 counter i: 14 total count: 22 listview1.item.key 16 counter i: 13 total count: 22 listview1.item.key 2counter i: 12 total count: 22 listview1.item.key 6counter i: 11 total count: 22 listview1.item.key 14 counter i: 10 total count: 22 listview1.item.key 18 counter i: 9total count: 22 listview1.item.key 19 counter i: 8total count: 22 listview1.item.key 5counter i: 7total count: 22 listview1.item.key 5counter i: 6total count: 22 listview1.item.key 3counter i: 5total count: 22 listview1.item.key 7counter i: 4total count: 22 listview1.item.key 8counter i: 3total count: 22 listview1.item.key 15 counter i: 2total count: 22 listview1.item.key 10 counter i: 1total count: 22 listview1.item.key 17 counter i: 0total count: 22 here is a screen shot of the two lists: listview1 is on the left, you will notice that the last two items are not on the random sorted list on the right. First, is it correct that you have twice the keys 13 and 5 in ListView1 and that keys 0 and 1 are missing? I tried to reproduce exactly your keys sequence but as soon as I want to add an identical key, I get a message and the programme stop immediatly. http://www.nabble.com/file/p23005690/ScreenshotBug.png Here is my code: PUBLIC SUB button4_click() DIM myArray AS Integer[ListView1.count] DIM a AS Integer DIM i AS Integer DIM c AS String FOR i = 0 TO ListView1.count - 1 STEP 1 myArray[i] = i NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 a = Int(Rnd(i + 1)) SWAP myArray[i], myArray[a] PRINT i, a NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 ' write items in listview2 - I have also tried running this counting up as well but it changed nothing. listview1.MoveTo(myArray[i]) TRY c = listview1.Item.Key TRY PRINT listview1.item.key , c, counter i: , i, total count: , listview1.Count TRY listview2.add(c, listview1.item.text) NEXT 'fGlobal.show 'FRolldice.Show 'ME.Hide END Sincerly JB SKaggs I run your code and it works quite fine! I think your missing items come from the duplicated keys 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 -- View this message in context: http://www.nabble.com/A-random-sort-of-listview-tp22919766p23008540.html Sent from the gambas-user mailing list
Re: [Gambas-user] A random sort of listview
jbskaggs ha scritto: FOR i = 0 TO ListView1.count - 1 STEP 1 myArray[i] = i NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 a = Int(Rnd(i + 1)) SWAP myArray[i], myArray[a] PRINT i, a NEXT Apart from the problem of missing records, there could be also a randomness problem in the algorithm... I am really not sure, but it seems that certain slots have more chances to be swapped than other - myarray[0] can be swapped COUNT times, while myarray[count-1] gets only a chance. I am not sure of what this signifies: it is true that the swap affects the values contained in slots, and not the slots themselves, but anyway there could be another method. One could also do this: for i = 0 to 1000 ' arbitrary value, large enough (100?) c1 = int(rnd(listview1.count)) c2 = int(rnd(listview1.count)) swap myarray[c1], myarray[c2] next This way, every slot gets the same chances to be swapped; for sufficient loops, it should make a good job... perhaps more random than before. And perhaps, as the algorthm is different, it could solve the problem of missing records (which I didn't understand...). Cheers, -- Doriano Blengino Listen twice before you speak. This is why we have two ears, but only one mouth. -- 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
Re: [Gambas-user] A random sort of listview
This works much better at getting a more shuffled list! Thanks Doriano Blengino wrote: jbskaggs ha scritto: FOR i = 0 TO ListView1.count - 1 STEP 1 myArray[i] = i NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 a = Int(Rnd(i + 1)) SWAP myArray[i], myArray[a] PRINT i, a NEXT Apart from the problem of missing records, there could be also a randomness problem in the algorithm... I am really not sure, but it seems that certain slots have more chances to be swapped than other - myarray[0] can be swapped COUNT times, while myarray[count-1] gets only a chance. I am not sure of what this signifies: it is true that the swap affects the values contained in slots, and not the slots themselves, but anyway there could be another method. One could also do this: for i = 0 to 1000 ' arbitrary value, large enough (100?) c1 = int(rnd(listview1.count)) c2 = int(rnd(listview1.count)) swap myarray[c1], myarray[c2] next This way, every slot gets the same chances to be swapped; for sufficient loops, it should make a good job... perhaps more random than before. And perhaps, as the algorthm is different, it could solve the problem of missing records (which I didn't understand...). Cheers, -- Doriano Blengino Listen twice before you speak. This is why we have two ears, but only one mouth. -- 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 -- View this message in context: http://www.nabble.com/A-random-sort-of-listview-tp22919766p23013584.html Sent from the gambas-user mailing list archive at Nabble.com. -- 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
Re: [Gambas-user] A random sort of listview
You were correct! There was a extra comma in the data file that was breaking the loop! BTW to you an everyone else if you celebrate it, Happy Easter! If not ignore me. ;) JB SKaggs Simonart Dominique wrote: Hi again :) May be you did not see the last sentence of my answer wich is written in 3 places? Your code is just fine and I don't have any problem with the ListViews. Of course, I had to initialize the ListView1 with my own data like this: FOR i = 0 to 21 ListView1.Add(Str(i), number Str(i)) NEXT After that, I run your code and all is fine! If you have problem, this is therefore with the content of your Listview1. I think it is not correct to have identical keys in a ListView because if I attempt to do it the program stop immediatly. I even don't understand how you could get this situation! Here is what I tried: FOR i = 0 to 21 SELECT CASE i CASE 0 ListView1.Add(5, number Str(i)) CASE 1 ListView1.Add(13, number Str(i)) CASE ELSE ListView1.Add(Str(i), number Str(i)) END SELECT NEXT this code stop with i=5 and say that the key is already used! Could you make a try with unique keys only? Plus, could you run it without TRY? Hope we could identify the error Dominique Simonart jbskaggs a écrit : Hi yourself! Here is the problem in the code: FOR i = 0 TO ListView1.Count - 1 ' write items in listview2 TRY PRINT myArray[i] listview1.MoveTo(myArray[i]) TRY c = listview1.Item.Key TRY PRINT myArray[i], listview1.item.key , c, counter i: , i, total count: , listview1.Count TRY listview2.add(c, listview1.item.text) NEXT When the counter i reaches Listview1.Count -1 it gives an error and does not write the last record! I have compensated by writing an extra dummy record to the data file- but how do I code this properly to write the whole list? I have tried several things and they just give different errors. Thanks JB Simonart Dominique wrote: Hi, jbskaggs a écrit : Okay I thought this was worked out but its still not working- I always end up losing the last two list items. Here is the console output from my print statements: This first list is printing i, a from this command SWAP myArray[i], myArray[a] 21 21 20 13 19 0 18 13 17 11 16 4 15 9 14 12 13 4 12 2 11 6 10 2 9 4 8 0 7 5 6 1 5 3 4 3 3 0 2 0 1 0 0 0 Here, you listed i and a but the interesting values are i and myArray[i] instead This list shows where listview1 is being copied into listview2: listview1.item.key 21 counter i: 21 total count: 22 listview1.item.key 13 counter i: 20 total count: 22 listview1.item.key 13 counter i: 19 total count: 22 listview1.item.key 20 counter i: 18 total count: 22 listview1.item.key 11 counter i: 17 total count: 22 listview1.item.key 4counter i: 16 total count: 22 listview1.item.key 9counter i: 15 total count: 22 listview1.item.key 12 counter i: 14 total count: 22 listview1.item.key 16 counter i: 13 total count: 22 listview1.item.key 2counter i: 12 total count: 22 listview1.item.key 6counter i: 11 total count: 22 listview1.item.key 14 counter i: 10 total count: 22 listview1.item.key 18 counter i: 9total count: 22 listview1.item.key 19 counter i: 8total count: 22 listview1.item.key 5counter i: 7total count: 22 listview1.item.key 5counter i: 6total count: 22 listview1.item.key 3counter i: 5total count: 22 listview1.item.key 7counter i: 4total count: 22 listview1.item.key 8counter i: 3total count: 22 listview1.item.key 15 counter i: 2total count: 22 listview1.item.key 10 counter i: 1total count: 22 listview1.item.key 17 counter i: 0total count: 22 here is a screen shot of the two lists: listview1 is on the left, you will notice that the last two items are not on the random sorted list on the right. First, is it correct that you have twice the keys 13 and 5 in ListView1 and that keys 0 and 1 are missing? I tried to reproduce exactly your keys sequence but as soon as I want to add an identical key, I get a message and the programme stop immediatly. http://www.nabble.com/file/p23005690/ScreenshotBug.png Here is my code: PUBLIC SUB button4_click() DIM myArray AS Integer[ListView1.count] DIM a AS Integer DIM i AS Integer DIM c AS
Re: [Gambas-user] A random sort of listview
-- 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
Re: [Gambas-user] A random sort of listview
Doriano Blengino a écrit : jbskaggs ha scritto: FOR i = 0 TO ListView1.count - 1 STEP 1 myArray[i] = i NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 a = Int(Rnd(i + 1)) SWAP myArray[i], myArray[a] PRINT i, a NEXT Apart from the problem of missing records, there could be also a randomness problem in the algorithm... I am really not sure, but it seems that certain slots have more chances to be swapped than other - myarray[0] can be swapped COUNT times, while myarray[count-1] gets only a chance. I am not sure of what this signifies: it is true that the swap affects the values contained in slots, and not the slots themselves, but anyway there could be another method. One could also do this: for i = 0 to 1000 ' arbitrary value, large enough (100?) c1 = int(rnd(listview1.count)) c2 = int(rnd(listview1.count)) swap myarray[c1], myarray[c2] next This way, every slot gets the same chances to be swapped; for sufficient loops, it should make a good job... perhaps more random than before. And perhaps, as the algorthm is different, it could solve the problem of missing records (which I didn't understand...). Cheers, = I resend my message because it seems the text is missing?! :-\ This is not really a problem. The Randomizing process could be explained like this: 1) You align a sorted card deck in front of you on a table 2) then, from all the cards on the table you take a random card in your hand so there is a place without card on the table 3) you put the last card on the table in this hole, so the hole is now at the last place 4) you continue by returning to 2 until you got all the cards in your hand Now you have a randomized deck of cards in your hand 5) you put the first card you got in the last place on the table and continue to do that until you have no card in your hand. Now the randomized deck of cards is on the table. If you examine carefully this process, you will remark that each time you filled a hole with the last remaining card on the table, you could place the card you got just before in the last place, because it is free! You only have to remember to not take these cards already selected. So you could do the 5) between 3) and 4) and this is exactly which is done by the SWAP instruction! It is true that some places will be selected several times, but their content change each time (there is another card on these places each time). In no way you could loose a card in this process!!. The JBskaggs' problem, I think, is that, before he starts the process, there are twice 2 identical cards in his deck and Gambas does not admit that, so when Gambas encounters the duplicated cards it reject them, (but since TRY is used, you don't see the error!!) and finally, 2 cards are missing. Hope this is clearer :) cheers 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
Re: [Gambas-user] A random sort of listview
Okay I thought this was worked out but its still not working- I always end up losing the last two list items. Here is the console output from my print statements: This first list is printing i, a from this command SWAP myArray[i], myArray[a] 21 21 20 13 19 0 18 13 17 11 16 4 15 9 14 12 13 4 12 2 11 6 10 2 9 4 8 0 7 5 6 1 5 3 4 3 3 0 2 0 1 0 0 0 This list shows where listview1 is being copied into listview2: listview1.item.key 21 counter i: 21 total count: 22 listview1.item.key 13 counter i: 20 total count: 22 listview1.item.key 13 counter i: 19 total count: 22 listview1.item.key 20 counter i: 18 total count: 22 listview1.item.key 11 counter i: 17 total count: 22 listview1.item.key 4counter i: 16 total count: 22 listview1.item.key 9counter i: 15 total count: 22 listview1.item.key 12 counter i: 14 total count: 22 listview1.item.key 16 counter i: 13 total count: 22 listview1.item.key 2counter i: 12 total count: 22 listview1.item.key 6counter i: 11 total count: 22 listview1.item.key 14 counter i: 10 total count: 22 listview1.item.key 18 counter i: 9total count: 22 listview1.item.key 19 counter i: 8total count: 22 listview1.item.key 5counter i: 7total count: 22 listview1.item.key 5counter i: 6total count: 22 listview1.item.key 3counter i: 5total count: 22 listview1.item.key 7counter i: 4total count: 22 listview1.item.key 8counter i: 3total count: 22 listview1.item.key 15 counter i: 2total count: 22 listview1.item.key 10 counter i: 1total count: 22 listview1.item.key 17 counter i: 0total count: 22 here is a screen shot of the two lists: listview1 is on the left, you will notice that the last two items are not on the random sorted list on the right. http://www.nabble.com/file/p23005690/ScreenshotBug.png Here is my code: PUBLIC SUB button4_click() DIM myArray AS Integer[ListView1.count] DIM a AS Integer DIM i AS Integer DIM c AS String FOR i = 0 TO ListView1.count - 1 STEP 1 myArray[i] = i NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 a = Int(Rnd(i + 1)) SWAP myArray[i], myArray[a] PRINT i, a NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 ' write items in listview2 - I have also tried running this counting up as well but it changed nothing. listview1.MoveTo(myArray[i]) TRY c = listview1.Item.Key TRY PRINT listview1.item.key , c, counter i: , i, total count: , listview1.Count TRY listview2.add(c, listview1.item.text) NEXT 'fGlobal.show 'FRolldice.Show 'ME.Hide END Sincerly JB SKaggs Simonart Dominique wrote: Simonart Dominique a écrit : 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 But if you prefer incremental loop, you could write: FOR i = 0 TO 199 A = Int(Rnd(i,200)) SWAP ... 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 -- 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
Re: [Gambas-user] A random sort of listview
Hi, jbskaggs a écrit : Okay I thought this was worked out but its still not working- I always end up losing the last two list items. Here is the console output from my print statements: This first list is printing i, a from this command SWAP myArray[i], myArray[a] 21 21 20 13 19 0 18 13 17 11 16 4 15 9 14 12 13 4 12 2 11 6 10 2 9 4 8 0 7 5 6 1 5 3 4 3 3 0 2 0 1 0 0 0 Here, you listed i and a but the interesting values are i and myArray[i] instead This list shows where listview1 is being copied into listview2: listview1.item.key 21 counter i: 21 total count: 22 listview1.item.key 13 counter i: 20 total count: 22 listview1.item.key 13 counter i: 19 total count: 22 listview1.item.key 20 counter i: 18 total count: 22 listview1.item.key 11 counter i: 17 total count: 22 listview1.item.key 4counter i: 16 total count: 22 listview1.item.key 9counter i: 15 total count: 22 listview1.item.key 12 counter i: 14 total count: 22 listview1.item.key 16 counter i: 13 total count: 22 listview1.item.key 2counter i: 12 total count: 22 listview1.item.key 6counter i: 11 total count: 22 listview1.item.key 14 counter i: 10 total count: 22 listview1.item.key 18 counter i: 9total count: 22 listview1.item.key 19 counter i: 8total count: 22 listview1.item.key 5counter i: 7total count: 22 listview1.item.key 5counter i: 6total count: 22 listview1.item.key 3counter i: 5total count: 22 listview1.item.key 7counter i: 4total count: 22 listview1.item.key 8counter i: 3total count: 22 listview1.item.key 15 counter i: 2total count: 22 listview1.item.key 10 counter i: 1total count: 22 listview1.item.key 17 counter i: 0total count: 22 here is a screen shot of the two lists: listview1 is on the left, you will notice that the last two items are not on the random sorted list on the right. First, is it correct that you have twice the keys 13 and 5 in ListView1 and that keys 0 and 1 are missing? I tried to reproduce exactly your keys sequence but as soon as I want to add an identical key, I get a message and the programme stop immediatly. http://www.nabble.com/file/p23005690/ScreenshotBug.png Here is my code: PUBLIC SUB button4_click() DIM myArray AS Integer[ListView1.count] DIM a AS Integer DIM i AS Integer DIM c AS String FOR i = 0 TO ListView1.count - 1 STEP 1 myArray[i] = i NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 a = Int(Rnd(i + 1)) SWAP myArray[i], myArray[a] PRINT i, a NEXT FOR i = ListView1.count - 1 TO 0 STEP -1 ' write items in listview2 - I have also tried running this counting up as well but it changed nothing. listview1.MoveTo(myArray[i]) TRY c = listview1.Item.Key TRY PRINT listview1.item.key , c, counter i: , i, total count: , listview1.Count TRY listview2.add(c, listview1.item.text) NEXT 'fGlobal.show 'FRolldice.Show 'ME.Hide END Sincerly JB SKaggs I run your code and it works quite fine! I think your missing items come from the duplicated keys 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
Re: [Gambas-user] A random sort of listview
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
Re: [Gambas-user] A random sort of listview
Hi Jussi, NO, there is no bug, it's my mistake!! I listed the myArray values just after the SWAP instruction. But, as you can see it in a preceeding answer to jbskaggs, this is a bad idea because in the forward FOR NEXT loop case, these values could change any time. You don't make this error :) so you got the good result! Since these values cannot change after they were selected in the Backward FOR NEXT loop case, my mistake had no consequence. We could consider this as a more efficient method since the correct result is available sooner! Dominique Simonart Jussi Lahtinen a écrit : 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
Re: [Gambas-user] A random sort of listview
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 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 -- 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 -- View this message in context: http://www.nabble.com/A-random-sort-of-listview-tp22919766p22930824.html Sent from the gambas-user mailing list archive at Nabble.com. -- 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
Re: [Gambas-user] A random sort of listview
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
Re: [Gambas-user] A random sort of listview
jbskaggs a écrit : I came up with a way to random sort listview. (Iuse this for random shuffling card slots on games) But it isnt efficient some pointers if you dont mind? I use two listviews one to sort from and one to sort to: I first copy all the listitems from list 1 to list 2 EXCEPT the key for listview2 items = the integer from my for next loop eg for i = 0 to listview1.count -1 step 1 listview2.add(i, listview1.item.text) next then with a for next loop based on the length of list1 I copy the listview.item key to a string A and listview.item.text to second string B Then I calculate a random number the count of listview2 and add that to a integer C thenadd it to listview2 listview2.add(nA, B,,C) nA creates a key of n1, n2, etc ... AFter the loop ends I run a second loop and run for i = 0 to listview1.count -1 step 1 listview.moveto(i) listview2.item.delete next This gives me a random sorted list in viewlist2 of viewlist1 with no duplicates etc- But is there a more efficient way of doing this? here is total code: PUBLIC SUB button3_click() DIM a AS String DIM d AS Integer DIM e AS Integer DIM i AS Integer DIM c AS Integer listview1.Clear 'clears the list FOR i = 0 TO 199 STEP 1 'number of items to add listview2.Add(i, Slot i) 'adds items to sort to list NEXT 'next item i = 0 FOR i = 0 TO 199 STEP 1 'number of items e = 0 listview1.MoveTo(i) 'goto item a = listview1.Item.Text 'get text PRINT a d = listview1.Key 'get key PRINT old key, d listview1.Item.Delete 'delete item (the cut part of cut and paste) e = Round(Rnd(200)) ' make sure old key doesNOT equal NEW Key PRINT e, the randomimzed number IF e = 200 THEN e = 199 listview2.Add(n d, n a,, e) 'adds cut items after randomly chosen item listview3.Add(n d, n a) PRINT new key, e NEXT 'next item down FOR i = 0 TO 199 STEP 1 'number of items listview2.MoveTo(i) listview2.Item.Delete NEXT ValueBox1.value = listview1.Count END JB SKaggs Hi, Let say you want to populate an array with 0-199 randomly (please note that I write this directly, so check carefully the syntax) dim myArray as integer[200] dim A as integer dim i as integer FOR i=0 to 199 A=int(Rnd(200-i)) ' I think we could use a Swap instruction ' instead of the 2 instructions below myArray[i]=A myArray[A]=i NEXT That's all If these numbers was the keys of the initial Listview you just have to populate the second Listview in the new order Hope this help 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
Re: [Gambas-user] A random sort of listview
Simonart Dominique a écrit : jbskaggs a écrit : I came up with a way to random sort listview. (Iuse this for random shuffling card slots on games) But it isnt efficient some pointers if you dont mind? I use two listviews one to sort from and one to sort to: I first copy all the listitems from list 1 to list 2 EXCEPT the key for listview2 items = the integer from my for next loop eg for i = 0 to listview1.count -1 step 1 listview2.add(i, listview1.item.text) next then with a for next loop based on the length of list1 I copy the listview.item key to a string A and listview.item.text to second string B Then I calculate a random number the count of listview2 and add that to a integer C thenadd it to listview2 listview2.add(nA, B,,C) nA creates a key of n1, n2, etc ... AFter the loop ends I run a second loop and run for i = 0 to listview1.count -1 step 1 listview.moveto(i) listview2.item.delete next This gives me a random sorted list in viewlist2 of viewlist1 with no duplicates etc- But is there a more efficient way of doing this? here is total code: PUBLIC SUB button3_click() DIM a AS String DIM d AS Integer DIM e AS Integer DIM i AS Integer DIM c AS Integer listview1.Clear 'clears the list FOR i = 0 TO 199 STEP 1 'number of items to add listview2.Add(i, Slot i) 'adds items to sort to list NEXT 'next item i = 0 FOR i = 0 TO 199 STEP 1 'number of items e = 0 listview1.MoveTo(i) 'goto item a = listview1.Item.Text 'get text PRINT a d = listview1.Key 'get key PRINT old key, d listview1.Item.Delete 'delete item (the cut part of cut and paste) e = Round(Rnd(200)) ' make sure old key doesNOT equal NEW Key PRINT e, the randomimzed number IF e = 200 THEN e = 199 listview2.Add(n d, n a,, e) 'adds cut items after randomly chosen item listview3.Add(n d, n a) PRINT new key, e NEXT 'next item down FOR i = 0 TO 199 STEP 1 'number of items listview2.MoveTo(i) listview2.Item.Delete NEXT ValueBox1.value = listview1.Count END JB SKaggs Hi, Let say you want to populate an array with 0-199 randomly (please note that I write this directly, so check carefully the syntax) dim myArray as integer[200] dim A as integer dim i as integer FOR i=0 to 199 A=int(Rnd(200-i)) ' I think we could use a Swap instruction ' instead of the 2 instructions below myArray[i]=A myArray[A]=i NEXT That's all If these numbers was the keys of the initial Listview you just have to populate the second Listview in the new order Hope this help Dominique Simonart Well, never write directly like that!! I make some mistake in my sample, here is a new one: DIM myArray AS Integer[200] DIM A AS Integer DIM i AS Integer 'initialize the array FOR i=0 TO 199 myArray[i]=i NEXT 'Randomize the order of the array FOR i = 199 TO 0 STEP -1 A = Int(Rnd(i + 1)) SWAP myArray[i], myArray[A] NEXT -- 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
Re: [Gambas-user] A random sort of listview
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 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 Simonart Dominique wrote: Simonart Dominique a écrit : jbskaggs a écrit : I came up with a way to random sort listview. (Iuse this for random shuffling card slots on games) But it isnt efficient some pointers if you dont mind? I use two listviews one to sort from and one to sort to: I first copy all the listitems from list 1 to list 2 EXCEPT the key for listview2 items = the integer from my for next loop eg for i = 0 to listview1.count -1 step 1 listview2.add(i, listview1.item.text) next then with a for next loop based on the length of list1 I copy the listview.item key to a string A and listview.item.text to second string B Then I calculate a random number the count of listview2 and add that to a integer C thenadd it to listview2 listview2.add(nA, B,,C) nA creates a key of n1, n2, etc ... AFter the loop ends I run a second loop and run for i = 0 to listview1.count -1 step 1 listview.moveto(i) listview2.item.delete next This gives me a random sorted list in viewlist2 of viewlist1 with no duplicates etc- But is there a more efficient way of doing this? here is total code: PUBLIC SUB button3_click() DIM a AS String DIM d AS Integer DIM e AS Integer DIM i AS Integer DIM c AS Integer listview1.Clear 'clears the list FOR i = 0 TO 199 STEP 1 'number of items to add listview2.Add(i, Slot i) 'adds items to sort to list NEXT 'next item i = 0 FOR i = 0 TO 199 STEP 1 'number of items e = 0 listview1.MoveTo(i) 'goto item a = listview1.Item.Text 'get text PRINT a d = listview1.Key 'get key PRINT old key, d listview1.Item.Delete 'delete item (the cut part of cut and paste) e = Round(Rnd(200)) ' make sure old key doesNOT equal NEW Key PRINT e, the randomimzed number IF e = 200 THEN e = 199 listview2.Add(n d, n a,, e) 'adds cut items after randomly chosen item listview3.Add(n d, n a) PRINT new key, e NEXT 'next item down FOR i = 0 TO 199 STEP 1 'number of items listview2.MoveTo(i) listview2.Item.Delete NEXT ValueBox1.value = listview1.Count END JB SKaggs Hi, Let say you want to populate an array with 0-199 randomly (please note that I write this directly, so check carefully the syntax) dim myArray as integer[200] dim A as integer dim i as integer FOR i=0 to 199 A=int(Rnd(200-i)) ' I think we could use a Swap instruction ' instead of the 2 instructions below myArray[i]=A myArray[A]=i NEXT That's all If these numbers was the keys of the initial Listview you just have to populate the second Listview in the new order Hope this help Dominique Simonart Well, never write directly like that!! I make some mistake in my sample, here is a new one: DIM myArray AS Integer[200] DIM A AS Integer DIM i AS Integer 'initialize the array FOR i=0 TO 199 myArray[i]=i NEXT 'Randomize the order of the array FOR i = 199 TO 0 STEP -1 A = Int(Rnd(i + 1)) SWAP myArray[i], myArray[A] NEXT -- 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 -- View this message in context: http://www.nabble.com/A-random-sort-of-listview-tp22919766p22922075.html Sent from the gambas-user mailing list archive at Nabble.com. -- This SF.net email is sponsored by: High Quality Requirements in a Collaborative Environment. Download a free trial of Rational Requirements Composer Now!