Welcome to the LimeSurvey Community Forum

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

Generating a random number from different ranges

  • asmusaj
  • asmusaj's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 month ago - 3 years 1 month ago #226958 by asmusaj
Hi all! 

I am struggling a little with the following: 

I want to present participants with a random scenario. Each time, I want to generate a random number to display a different scenario. 
However, I want to omit some numbers. Therefore, rand() does not work. 

E.g.: I want to display a random number between 850-900 OR 960-990 OR 1010-1040 OR 1100-1200

Can this be done? 
Any help is highly appreciated! :) 
Last edit: 3 years 1 month ago by asmusaj.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 month ago - 3 years 1 month ago #226960 by Joffm
Oh, this editor. 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 1 month ago by Joffm.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 month ago #226961 by Joffm
Hi, asmusaj,
at first: Why didn't you answer the initial questions?

herefore, rand() does not work.


Of course it does.

There are several options.
1. Your ranges cover 214 possible numbers.
a. Create a random number (r1) form 1-214: {if(is_empty(r1),rand(1,214),r1)}
b. Create your final random number  (r2) in a nested IF-function : {if(r1<52,r1+849,if(r1<83,r1+908,if(r1<114,r1+927,r1+986)))}
In my opinion it is the best one.

2. Use a random number to define the range and a second to get the final number
a. create a random number from 1-4 (r3): {if(is_empty(r3),rand(1,4),r3)}
b. create your final random number (r4) in again in a nested IF: {if(r3==1,if(is_empty(r4),rand(850,900),r4),if(r3==2,if(is_empty(r4),rand(960,990),r4),if(r3==3,if(is_empty(r4),rand(1010,1040),r4),if(is_empty(r4),rand(1100,1200),r4))))}

3. Solution with javascript
create a question of type "short text" and enter the following script in source code mode
Code:
<script type="text/javascript" charset="utf-8">
 
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;
 
  // While there remain elements to shuffle...
  while (0 !== currentIndex) {
 
    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
 
    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
 
  return array;
}
 
 
    $(document).on('ready pjax:scriptcomplete',function(){
// Fill the array
      var arr = [];
      for (var i = 850; i < 901; i++) {
         arr.push(i);
      }
      for (var i = 960; i < 991; i++) {
         arr.push(i);
      }
      for (var i = 1010; i < 1041; i++) {
         arr.push(i);
      }
      for (var i = 1100; i < 1201; i++) {
         arr.push(i);
      }
 
      arr = shuffle(arr);
      anumbers = arr.slice(0,1).join('');
      $('#question{QID} input[type="text"]').val(anumbers);
 
//    After testing remove the slashes to hide the question
//    $('#question{QID}').hide();
   });
</script>

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: asmusaj

Please Log in to join the conversation.

  • asmusaj
  • asmusaj's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 month ago #226965 by asmusaj
Hi Joffm!

Thanks so much for the lengthy answer and for showing all possible options. I was not aware rand() could be so versatile if used correctly. I just tried all options in my survey and all work like a charm!

This would have taken me forever (if so) to figure out myself. Sunday saved :)

Thanks a lot!!!

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose