Shuffle Table
Shuffle Table
A Table-Function that will shuffle the Table contents by random would be helpfull.
I´m not 100% sure, maybe this can be reached by using:
Sort(array[, sortfunc])
with a randomizer sortfunc.
I´m not 100% sure, maybe this can be reached by using:
Sort(array[, sortfunc])
with a randomizer sortfunc.
Re: Shuffle Table
Code: Select all
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Sort(nums, Function(a, b) Return(Rnd(1)) EndFunction)
For k = 0 To 9
DebugPrint(nums[k])
Next
Re: Shuffle Table
I don't really understand why, but the code Bugala suggested gives the exact same new list every time (so not really random).
This code does seem to work, though:
This code does seem to work, though:
Code: Select all
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
?Sort(nums, Function(a, b) Return(Rnd(2)) EndFunction)
For k = 0 To 9
error=?DebugPrint(nums[k])
If error<>Nil Then DebugPrint("Error")
Next
-
- Posts: 475
- Joined: Fri May 15, 2015 5:15 pm
- Location: Waterville, Minnesota USA
Re: Shuffle Table
Rnd(1) gives 1 possible integer value of 0
Rnd(2) gives 1 of 2 possible values: 0 or 1. This is due to the range select value being applied as a modulo operation.
Rnd(2) gives 1 of 2 possible values: 0 or 1. This is due to the range select value being applied as a modulo operation.
I'm on registered MorphOS using FlowStudio.
Re: Shuffle Table
Hmm... there seems to be something hazy here... I just stumbled upon an issue like this.
If you use this code:
It sometimes goes through fine, but often gives an error: "Invalid order function for sorting!"
Why it fails even though it should get valid values of 0 or 1 always?
Is this why PEB used the question mark in front of his Sort line? For a workaround for a bug?
If you use this code:
Code: Select all
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Sort(nums, Function(a, b) Return(Rnd(2)) EndFunction)
Why it fails even though it should get valid values of 0 or 1 always?
Is this why PEB used the question mark in front of his Sort line? For a workaround for a bug?
-
- Posts: 475
- Joined: Fri May 15, 2015 5:15 pm
- Location: Waterville, Minnesota USA
Re: Shuffle Table
Curiously, copying the two parameters to dummy variables alleviates the Sort check.
Code: Select all
@DISPLAY 0, {HIDDEN = True}
nums = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}
Function Shuffler(x, y)
Local a=x, b=y
Return(Rnd(2) > 0)
EndFunction
Sort(nums, Shuffler)
ListRequest("Shuffled", "Click OK to continue or Cancel to also continue", nums)
I'm on registered MorphOS using FlowStudio.
Re: Shuffle Table
@SamuraiCrow
I still get the errors with your code too...
I still get the errors with your code too...
-
- Posts: 475
- Joined: Fri May 15, 2015 5:15 pm
- Location: Waterville, Minnesota USA
Re: Shuffle Table
This one won't crash and it includes another version for generic tables tested with a Unicode key value.
Code: Select all
@DISPLAY 0, {HIDDEN = True}
@VERSION 7,0
/*
* Table shuffler
*
* Parameter:
* a Table to shuffle
*
* Returns:
* Shuffled table
*/
Function p_ShuffleTable(a)
Local key, content, k, max, iter=Nil, Nil, {}, 0, Nil
; create an array of the keys
Repeat
iter = NextItem(a, iter)
If GetType(iter) = #NIL Then Break
k[max] = iter
max = max + 1
Forever
; Randomize table
For key, content In Pairs(a)
iter = k[ Rnd(max) ]
; no point in skipping redundant keys if they are Unicode
; the CompareStr function is probably more expensive than the swap
a[key] = a[iter]
a[iter] = content
Next
Return(a)
EndFunction
/*
* Array shuffler
*
* Parameter:
* a array to shuffle
*
* Returns:
* Shuffled array
*/
Function p_ShuffleArray(a)
Local key, content, iter, max=Nil, Nil, Nil, ListItems(a)
; Randomize array
For key, content In IPairs(a)
iter = Rnd(max)
If (iter <> key)
a[key] = a[iter]
a[iter] = content
EndIf
Next
Return(a)
EndFunction
nums = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}
letters = {}
strings={}
letters["a"] = "apple"
letters["b"] = "baking"
letters["c"] = "catapult"
letters["d"] = "destination"
letters["e"] = "eggplant"
letters["f"] = "farmer"
letters[0] = "what?"
letters[chr(228, #ENCODING_UTF8)] = "etc."
ListRequest("Shuffled", "Click OK or Cancel to continue", p_ShuffleArray(nums) )
letters = p_ShuffleTable(letters)
ForEach(letters, Function(k,v)
InsertItem(strings, k.."="..v)
EndFunction)
Sort(strings, Function(a,b)
CompareStr(a,b)
EndFunction)
ListRequest("Shuffled", "Click OK or Cancel to continue", strings)
I'm on registered MorphOS using FlowStudio.
- airsoftsoftwair
- Posts: 5443
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Shuffle Table
Because it's not consistent. Using Rnd() inside the sort callback is a very bad idea because it will confuse the sort algorithm. Imagine it first compares 1<2 and the result is TRUE and the next time it does this comparison the result is FALSE. This will mess up things.