Shuffle Table

Feature requests for future versions of Hollywood can be voiced here
Post Reply
User avatar
fingus
Posts: 151
Joined: Fri Sep 16, 2011 9:53 am

Shuffle Table

Post by fingus » Mon Sep 23, 2019 11:27 am

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.

Bugala
Posts: 823
Joined: Sun Feb 14, 2010 7:11 pm

Re: Shuffle Table

Post by Bugala » Mon Sep 23, 2019 11:42 am

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
It is not explained very clearly in the sort documentation, but function needs to return either FALSE or TRUE (which can also be 0 or 1).

PEB
Posts: 261
Joined: Sun Feb 21, 2010 1:28 am

Re: Shuffle Table

Post by PEB » Mon Sep 23, 2019 7:00 pm

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:

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

SamuraiCrow
Posts: 241
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Shuffle Table

Post by SamuraiCrow » Tue Sep 24, 2019 2:36 pm

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.

Post Reply