Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Randomization using chance.js

  • copsrtops
  • copsrtops's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #110455 by copsrtops
Randomization using chance.js was created by copsrtops
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.

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:
Code:
<html>
<head></head>
<body>
<script type="text/javascript" src="https://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:
Code:
<script type="text/javascript" src="https://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 topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
10 years 5 months ago #110466 by DenisChenu
Replied by DenisChenu on 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 support - Plugins, theme and development .
I don't answer to private message.
The topic has been locked.
  • copsrtops
  • copsrtops's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #110472 by copsrtops
Replied by copsrtops on 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
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
10 years 5 months ago #110476 by DenisChenu
Replied by DenisChenu on 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 support - Plugins, theme and development .
I don't answer to private message.
The topic has been locked.
  • copsrtops
  • copsrtops's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #110480 by copsrtops
Replied by copsrtops on 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.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
10 years 5 months ago #110481 by DenisChenu
Replied by DenisChenu on 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 support - Plugins, theme and development .
I don't answer to private message.
The following user(s) said Thank You: copsrtops
The topic has been locked.
  • copsrtops
  • copsrtops's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 4 months ago #110487 by copsrtops
Replied by copsrtops on 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.
The topic has been locked.
More
10 years 4 months ago #110494 by Ben_V
Replied by Ben_V on 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)
Code:
<script type="text/javascript" src="https://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)
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose