Randomization using chance.js

More
3 years 2 months ago #110455 by copsrtops
copsrtops created the topic: Randomization using chance.js
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.

Please Log in to join the conversation.

More
3 years 2 months ago #110466 by DenisChenu
DenisChenu replied the topic: Randomization using chance.js
You are super admin ?

Can you provide a test link (little survey with this system (only one question is the best)).

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
3 years 2 months ago #110472 by copsrtops
copsrtops replied the topic: Randomization using chance.js
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...5994.lss
File Size:12 KB
Attachments:

Please Log in to join the conversation.

More
3 years 2 months ago #110476 by DenisChenu
DenisChenu replied the topic: Randomization using chance.js
You can give a link ? It's speedest for me if you give s directly a link ...

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
3 years 2 months ago #110480 by copsrtops
copsrtops replied the topic: Randomization using chance.js
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.

Please Log in to join the conversation.

More
3 years 2 months ago #110481 by DenisChenu
DenisChenu replied the topic: Randomization using chance.js
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

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
The following user(s) said Thank You: copsrtops

Please Log in to join the conversation.

More
3 years 2 months ago #110487 by copsrtops
copsrtops replied the topic: Randomization using chance.js
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.

Please Log in to join the conversation.

More
3 years 2 months ago #110494 by Ben_V
Ben_V replied the topic: Randomization using chance.js
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

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now