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

Reply via email to