[Gambas-user] A random sort of listview
Doriano Blengino
doriano.blengino at ...1909...
Mon Apr 13 08:46:04 CEST 2009
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."
More information about the User
mailing list