Welcome to the LimeSurvey Community Forum

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

Randomisation using PHP 7.0 or above

  • gorp
  • gorp's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 1 month ago #194768 by gorp
Hi,
I've used rand() like below in an equation question to get a random number or 1 or 2. This works fine:

{if(is_empty(condition1),rand(1,2),condition1)} .

However, I would now like to use random_int, but when I do that it says Undefined function even though the website is using PHP 7.0. This is what I now have.
{if(is_empty(condition1),random_int(1,2),condition1)}

Can someone please help?
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 month ago #194769 by Joffm
Replied by Joffm on topic Randomisation using PHP 7.0 or above
Please, read the manual about implemented functions
manual.limesurvey.org/Expression_Manager#Implemented_Functions

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
More
4 years 1 month ago #194774 by jelo
Replied by jelo on topic Randomisation using PHP 7.0 or above

gorp wrote: However, I would now like to use random_int, but when I do that it says Undefined function even though the website is using PHP 7.0.

The ExpressionScript/ExpressionManager functions are using PHP functions, but not exposing all functions of PHP.

github.com/LimeSurvey/LimeSurvey/blob/26...s/em_core_helper.php

If you want to use random_int, you would need to submit a feature request or modify the LimeSurvey code.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 month ago #194776 by Joffm
Replied by Joffm on topic Randomisation using PHP 7.0 or above
Well,
without having to hack the core, you may create a question of type short text and do an ajax call.

Like this:
Code:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
 
  $.post('https://www.myServer/getRandom.php' ,{ min: "[minValue]", max: "[maxValue]"} function(data) {
 
                   $('#question{QID} input[type="text"]').val(data)
 
      });
//  $('#question{QID}').hide();
  });
</script>

And in the called php script you grab "min" and "max" and echo random_int(min,max);.

Just an idea.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 month ago #194782 by tpartner
Replied by tpartner on topic Randomisation using PHP 7.0 or above
Or use chance.js - chancejs.com/

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 month ago #194838 by DenisChenu
Replied by DenisChenu on topic Randomisation using PHP 7.0 or above

jelo wrote: ....
If you want to use random_int, you would need to submit a feature request or modify the LimeSurvey code.

No,

Not if you use 4.X : manual.limesurvey.org/ExpressionManagerStart

See this sample : github.com/LimeSurvey/LimeSurvey/tree/ma...mo/demoAddEmFunction

And since random_int is a PHP function, you can set the function as "static" : see github.com/LimeSurvey/LimeSurvey/tree/ma...lugins/statFunctions

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose