I'm trying to create random test answers. Using a unique ID (Text) - Below randomizes the list only once when I use it. If I reload page it doesn't randomize again.
Also - if it is a True False Answer of only 2 choices. It doesn't work.
Any thoughts folks? Or is there an easier way to do this. I know I can do it with numbers easily, but I have a preference for unique answer ID in text)
<cfset strList = "rttt,ddde,ffss,gggd" /> - works only once
<cfset strList = "True,False" /> - doesn't work
<!---
Create a struct to hold the list of selected numbers. Because
structs are indexed by key, it will allow us to not select
duplicate values.
--->
<cfset objSelections = {} />
<!--- Now, all we have to do is pick random numbers until our
struct count is the desired size (4 in this demo).
--->
<cfloop condition="(StructCount( objSelections ) LT 4)">
<!--- Select a random list index. --->
<cfset intIndex = RandRange( 1, ListLen( strList ) ) />
<!---
Add the random item to our collection. If we have
already picked this number, then it will simply
overwrite the previous and the StructCount() will
not be changed.
--->
<cfset objSelections[ ListGetAt( strList, intIndex ) ] = true />
</cfloop>
<cfoutput>
<!--- Output the list collection. --->
<cfset newlist = "#StructKeyList( objSelections )#">
#newlist#
</cfoutput>
The reason it does not re-randomize your list after a reload is because Structs are not ordered. You are better to use an array or even a java hashtable. If I understand correctly, you are just trying to take a list, and output a re-ordered version of the list? It's probably been answered before in more suscint forms than this, but here is a way, if I have understood your requirement correctly:
<cfset strList = "rttt,ddde,ffss,gggd" />
<cfset newlist = "">
<cfloop condition="ListLen(strList)">
<cfset intIndex = RandRange( 1, ListLen( strList ) ) />
<cfset newlist = ListAppend(newlist, ListGetAt(strList, intIndex))>
<cfset strList = ListDeleteAt(strList, intIndex)>
</cfloop>
<cfoutput>#newlist#</cfoutput>