samedi 13 octobre 2018

ColdFusion - Generate Random Text(ID) Answer List

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>




Aucun commentaire:

Enregistrer un commentaire