Welcome, Guest
Username: Password: Remember me

TOPIC: Randomization using chance.js

Randomization using chance.js 4 months 2 days ago #110455

  • copsrtops
  • copsrtops's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 6
  • Karma: 0
Hi everyone

I have a question about random number generation.

I have read in the manual and on the forum that the standard ways to generate random numbers are to use the rand() function in the equation manager, or the Math.random()JavaScript function.

Recently I came across ‘Chance’ which is a nice alternative method for generating random numbers that is meant to be better than rand() or Math.random() as it is based on Mersenne Twister.

http://chancejs.com/

I can get chance.js to generate random numbers with ease when I use it outside of lime survey.
For example, the follow code successfully generates a random integer:
<html>
<head></head>
<body>
<script type="text/javascript" src="http://chancejs.com/chance.min.js" charset="utf-8"></script>
<script type="text/javascript">
	alert(chance.integer());
</script>
</body>
</html>

However, I am unable to get chance.js to work with limesurvey.
For example, I created a text display question in limesurvey and then (in source mode) added the following script:
<script type="text/javascript" src="http://chancejs.com/chance.min.js" charset="utf-8"></script>
<script type="text/javascript">
	alert(chance.integer());
</script>

When I then execute the survey, no alert pops up on screen containing the random integer.

I have experimented with various settings to try and get chance.js to generate random numbers from within limesurvey. For example, I have tried various question types (Eg short text instead of text display) and also tried including the chance.js file in the head rather than the body of the HTML (by editing limesurvey templates) but nothing has worked.

As the script will successfully generate random numbers when used outside of limesurvey, but does is not working when used inside limsurvey, I assume there is some sort of interaction going on between limesurvey and chance.js that is causing the problem.

Does anyone have any advice as to how I could overcome this problem so that I can use chance.js rather than rand() or Math.random() to generate random values?

Thanks

David

PS I am using LS 2.05+ on an Apache server.
The administrator has disabled public write access.

Randomization using chance.js 4 months 2 days ago #110466

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 6472
  • Thank you received: 855
  • Karma: 254
You are super admin ?

Can you provide a test link (little survey with this system (only one question is the best)).
The administrator has disabled public write access.

Randomization using chance.js 4 months 2 days ago #110472

  • copsrtops
  • copsrtops's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 6
  • Karma: 0
Hi Denis

Attached is a one question demo survey. It's just a text display question that is meant to return an alert with a random integer.


File Attachment:

File Name: limesurvey_survey_845994.lss
File Size: 12 KB
The administrator has disabled public write access.

Randomization using chance.js 4 months 2 days ago #110476

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 6472
  • Thank you received: 855
  • Karma: 254
You can give a link ? It's speedest for me if you give s directly a link ...
The administrator has disabled public write access.

Randomization using chance.js 4 months 2 days ago #110480

  • copsrtops
  • copsrtops's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 6
  • Karma: 0
Here's a link to a demo survey.

There's a single short-text question, a random integer should appear in the box but it does not.
The administrator has disabled public write access.

Randomization using chance.js 4 months 2 days ago #110481

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 6472
  • Thank you received: 855
  • Karma: 254
Your website is in HTTPS, then your browser (Chrome and FF up to 23) block link to non https javascript content.

Please : sue your browser javascript console to track donc error.

Another thing : it's better for chance dev if you don't use his own website : it's not a public CDN (Developper don't put information like this).

Then: upload the chance.js file to your own website (with https) and use this link.

Denis
The administrator has disabled public write access.
The following user(s) said Thank You: copsrtops

Randomization using chance.js 4 months 1 day ago #110487

  • copsrtops
  • copsrtops's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 6
  • Karma: 0
Thanks Denis.

For proper surveys I always use a CDN or upload the external js files to my own server. I’d only linked to the developer site this time around while testing the script. I didn't realise that would be cause errors due to the chance website being insecure.

I changed the script src to a copy of on my own server (over HTTPS) which has resolved the problem.

Thanks so much for your help.
The administrator has disabled public write access.

Randomization using chance.js 4 months 1 day ago #110494

  • Ben_V
  • Ben_V's Avatar
  • OFFLINE
  • Platinum Lime
  • Posts: 1125
  • Thank you received: 259
  • Karma: 79
If it could be usefull for some other LS users :) :

1) Get a random "pin" using EM : related post

2) Prefill a numerical question using chance.js (example for a 7 digits number)
<script type="text/javascript" src="http://chancejs.com/chance.min.js" charset="utf-8"></script>
<script type="text/javascript">
 
 var pin= chance.integer({ min: 1000000, max: 9999999 });
 
 jQuery(document).ready(function() {
	$(".numeric:eq(0) input.text").val(pin);
	$(".numeric:eq(0) input.text").attr('readonly','readonly');
 });
</script>
Benoît

goo.gl/Bw5iM => Recherche GG dans le forum français (remplacer "exemple" dans la barre de recherche)
goo.gl/WX8PH => GG search for english forum (Replace "example" in the search bar)
goo.gl/IxiGu => Búsqueda en el foro en español (Cambiar "ejemplo" en la barra de...
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.256 seconds
Donation Image