Welcome to the LimeSurvey Community Forum

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

Generate Random Number, use it & save it in answers

  • J.Auer
  • J.Auer's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 1 week ago - 1 month 1 week ago #259451 by J.Auer
Your LimeSurvey version: 6.4.12

Hello!

I'm in the process of setting up an initial assignment for participants, giving each either a 0 or 1. This will dictate which link opens in fullscreen upon pressing a button. (In my context, these correspond to Game Version 1 or Game Version 2, in this example Bing & Google).
I thought this would be the most efficient way to get balanced a 50/50 chance for both links.I've managed to implement this successfully using the code provided below. However, I'm now interested in storing the assigned number (either 0 or 1) to track this in the final responses.I've tried to set it as an answer to a follow-up question, but can't get it to work.

What steps should I take next, or is there a more effective approach to address my requirement?


relevant code:
Code:
<p>Random Number:</p>
<p id="randomValue"> </p>
 
<script>
    var randomValue = rand(0, 1);
    document.getElementById("randomValue").innerText = "Zufallszahl: " + randomValue;
  
    function openFullscreenPopup(url) {
        var width = screen.width;
        var height = screen.height; 
        var popupWindow = window.open(url, 'popupWindow', 'width=' + width + ', height=' + height + ', left=0, top=0');
        if (window.focus) {
            popupWindow.focus();
        }
        return false;
    }
 
    function openVersion() {
        var gameVersion = (randomValue < 0.5) ? 'https://www.bing.com' : 'https://www.google.com';
 
        openFullscreenPopup(gameVersion);
    }
 
</script>
<button class="my-button" onclick="openVersion(); return false;">Open in Fullscreen</button>
Last edit: 1 month 1 week ago by J.Auer.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 1 week ago #259454 by tpartner
If your code is in the source of a sort-text question, you can load that input like this:

Code:
$('#question{QID} :text.form-control:eq(0)').val(randomValue).trigger('keyup);

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • J.Auer
  • J.Auer's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 1 week ago #259460 by J.Auer
Thank you for your input!
I'm not quite sure how to implement that, though. I've created a sorting question using my code. In the first answer, I included your code and tried it like this for example:
Code:
<script>  $('#question{31} :text.form-control:eq(0)').val(randomValue).trigger('keyup);  </script>
 However, it doesn't seem to work, so I must be doing something wrong for sure. 
I would greatly appreciate if you could provide a more detailed instructions on how to implement this. Thanks in advance!

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 1 week ago - 1 month 1 week ago #259463 by Joffm
You should not change {QID}
{QID} is a system variable that always returns the actual QuestionID.
Same wirh
{SID}: Survey ID
{GID}: Group ID
{SAVEDID}: Response ID
and many more.

And put it at the end of the script where you generate the randomvalue, e.g. before "openFullscreen".

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 month 1 week ago by Joffm.
The following user(s) said Thank You: tpartner

Please Log in to join the conversation.

  • J.Auer
  • J.Auer's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 1 week ago - 1 month 1 week ago #259535 by J.Auer
Thank you for your explanation! That makes sense now!However, I'm still encountering difficulties in making this work. After adding the provided line of code, I noticed that the RandomValue is no longer displayed in the text itself, and the button no longer opens the link. Additionally, the RandomValue is not being logged in the first answer. I've attempted this with different question types as well. Is there still something I might be missing?

Code:
<p>Here is the Random Number:</p>
 
<p id="randomValue"> </p>
<script>
    var randomValue = rand(0, 1);
 
    document.getElementById("randomValue").innerText = "Random Number: " + randomValue;
  
     $('#question{QID} :text.form-control:eq(0)').val(randomValue).trigger('keyup);
 
    function openFullscreenPopup(url) {
        var width = screen.width;
        var height = screen.height;
        var popupWindow = window.open(url, 'popupWindow', 'width=' + width + ', height=' + height + ', left=0, top=0');
        if (window.focus) {
            popupWindow.focus();
        }
        return false;
    }
 
    function openVersion() {
        var gameVersion = (randomValue < 0.5) ? 'https://www.bing.com' : 'https://www.google.com';
        openFullscreenPopup(gameVersion);
    }
</script><button class="my-button" onclick="openVersion(); return false;">Open in Fullscreen</button>


I also attached a screenshot of how this looks in the preview
 
Last edit: 1 month 1 week ago by J.Auer.

Please Log in to join the conversation.

  • J.Auer
  • J.Auer's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 1 week ago - 1 month 1 week ago #259552 by J.Auer
Here's a fix ChatGPT gave me. It seems to work properly now, not sure if this is the best solution, but it fixes my problem!

Code:
<p>Here is the Random Number:</p>
 
<p id="randomValue"> </p>
<script>
 
    var randomValue;
 
    $(document).ready(function() {
        randomValue = rand(0, 1);
        document.getElementById("randomValue").innerText = "Random Number: " + randomValue;
        
        $('#question{QID} :text.form-control:eq(0)').val(randomValue).trigger('keyup');
    });
 
    function openFullscreenPopup(url) {
        var width = screen.width;
        var height = screen.height;
        var popupWindow = window.open(url, 'popupWindow', 'width=' + width + ', height=' + height + ', left=0, top=0');
        if (window.focus) {
            popupWindow.focus();
        }
        return false;
    }
 
    function openVersion() {
        var gameVersion = (randomValue < 0.5) ? 'https://www.bing.com' : 'https://www.google.com';
        openFullscreenPopup(gameVersion);
    }
</script><button class="my-button" onclick="openVersion(); return false;">Open in Fullscreen</button>
Last edit: 1 month 1 week ago by J.Auer.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose