Welcome to the LimeSurvey Community Forum

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

Generate 2 unique random numbers between 1 and 4

  • john_bons
  • john_bons's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 10 months ago #217005 by john_bons
Hi,

I would like to generate 2 unique random numbers between 1 and 4. i..e 
random 1 = any random number between 1 and 4 = rand(1, 4)
random 2 = any random number between 1 and 4, other than random 1.

could you suggest me a solution?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 10 months ago #217011 by tpartner

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 10 months ago - 3 years 10 months ago #217021 by Joffm
Hi,
I saw in the linked thread "No EM solution".

Here is one:
1. equation (rand1) {if(is_empty(rand1,rand(1,4),rand1)}  - the usual way to create a random number.
2. equation (base2) {str_replace(rand1,"","1234")} - remove the first random number from this string
3. equation (rand2) {if(is_empty(rand2),substr(base2,rand(0,2),1),rand2)} - select randomly one character from the remaining string.

Now rand1 and rand2 are two different random numbers.

Just to show that it is possible.
Of course, this only works with numbers between 1 and 9.
Else it has to be more sophisticated.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 10 months ago by Joffm.
The following user(s) said Thank You: holch
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 10 months ago - 3 years 10 months ago #217062 by Joffm
And there is an one-equation solution.

If you consider that there are only 6 (4 over 2) different combinations
1-2, 1-3, 1-4, 2-3, 2-4, 3-4

one random number between 1 and 6 is sufficient

And the questions than get the relevance equations:
Q1: random < 4
Q2: random==1 or random==4 or random==5
Q3: random==2 or random==4 or random==6
Q4: random==3 or random>4

This is to show that remembering a bit of mathematics at school some things may become easy.

Joffm

PS.
Of course I know that this gets very complicated when trying to select 3 out of 10. (10 over 3 = 120)

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 10 months ago by Joffm.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 10 months ago #217077 by tpartner
Show off! :)

I can't spell mathamatics. :)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose