[Gambas-user] A random sort of listview
Doriano Blengino
doriano.blengino at ...1909...
Sun Apr 12 09:51:21 CEST 2009
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."
More information about the User
mailing list