1
$\begingroup$

I have the following function for making a RadioButtonBar with randomized choices. The code is

makeQuestionRandom[title1_, choices1_, appearance1_, question1__] :=

 DynamicModule[{title = title1, choices = choices1, 
   appearance = appearance1, question = question1, selection = 0},
  Panel[
   Grid[{
     {question},
     {RadioButtonBar[ Dynamic[selection], RandomSample[choices], 
       Appearance -> "Vertical" -> appearance,
       ImageMargins -> {{0, 0}, {25, 0}}
       ](*close radiobutton bar*)
      }(*close radiobutton bar entry for grid*),
     {Dynamic[
       Switch[selection, 1, Style["Correct!", Darker[Green, .25]], 0, 
        " ", _, Style["Wrong ", Darker[Red]]]]
      }(*close dynamic text row*)
     },(*close grid matrix*)

    Alignment -> Left,
         Spacings -> {{2, 2}, {2, 2}}
    ],(*close grid bracket*)
   title
   ](*close panel bracket*)
  ](*close dynamicmodule bracket*)

The output looks like

enter image description here

What I would like is for this output to 'execute' each time the notebook is opened. I'm trying to create notebooks that are quizzes and the questions will look like this. Hence my desire for having the choices randomized each time the notebook is opened without showing the code that generated the output.

$\endgroup$
  • $\begingroup$ Could you mark all of the question cells as initialisation cells [Cells | Cell Properties | Initialization Cell] and and convert to a CDF. When the CDF is opened I believe it will run all of the initialisation cells. Test it for a couple of questions and see if it works. $\endgroup$ – Edmund Apr 22 '15 at 1:41
  • $\begingroup$ I was not able to make this work. Thanks for the suggestion. I hadn't considered anything along these lines and didn't know about initialization cell properties. $\endgroup$ – sykh Apr 22 '15 at 4:19
  • 1
    $\begingroup$ DynamicModule has an option Initialization, of which the contents is executed each time when the DynamicModule is first displayed, so in particular when the notebook is opened. Would that be helpful for you? $\endgroup$ – Fred Simons Apr 22 '15 at 6:00
  • $\begingroup$ Yes, that works. Edmund was on the right track. I just didn't try initialization with DynamicModule. When I read the documentation I thought "first displayed" literally meant when it was first displayed throughout all history. Thank you for the suggestion. $\endgroup$ – sykh Apr 22 '15 at 15:59
2
$\begingroup$

As Fred has suggested, Initialization is the way to go, and here is a minimal example:

DynamicModule[{answer, answers = Range@10, order},
  Column[{
    "Pick 1:",
    Dynamic[RadioButtonBar[Dynamic@answer, answers[[order]]], 
      TrackedSymbols :> {order}]
  }]
  ,
  Initialization :> ( order = RandomSample@Range@Length@answers; ),
  UnsavedVariables :> {answer}

]

enter image description here

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.