HOME ➔ SUPPORT ➔ Community ➔ General CourseLab issues ... Rounding
Rounding
  View type:
This is my first post and I must say that I have been very impressed with CourseLab[;)]. I have found the Math.random() function very useful however I have had to use 10 IF statements to get random numbers from 0 to 9. I now want to do random integers from 0 to 99 and obviously writing out 100 IF statements is not appealing. Is there a round or integer function I can use? I tried using % for remainder (as described in the help files) but couldn't get it to work at all. Java has a round function but does Courselab? Any ideas gratefully received.
 
Hi, Kev,

the only way to do that now is to use the JavaScript action. CourseLab variables are stored in global JavaScript arrays (one for slide scope and other for module scope). Let it be slide var for example. Then you will need this action sequence:

VARIABLE (my_var, Math.random()*100)
JAVASCRIPT {

g_arSlideVars['my_var'] = Math.round(g_arSlideVars['my_var']);

}

That's all - my_var is rounded. If you var has entire module scope, then name of the array must be changed to g_arVars.
 
 
Hi Slav

Thank you very much indeed - you have saved me a lot of time. I could not get the javascript to run at first but then I noticed in another post of yours that square brackets are needed round the variables. This worked fine:

g_arSlideVars['my_var']=Math.round(g_arSlideVars['my_var']);

Thanks again and I think that courselab is fantastic!
Kevin[:)]
 
 
 
Oops! My fault [:)] Of course there must be square brackets - it's a collection.
I fixed it in previous message too.
Subject:
Message options
No additional options