Welcome to the LimeSurvey Community Forum

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

Normal Distribution in Random Number

  • KatharinaFietz
  • KatharinaFietz's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 weeks 5 days ago #266955 by KatharinaFietz
Normal Distribution in Random Number was created by KatharinaFietz
Good morning,

I am trying to create a random number between 0.75 and 1.25 but as a normal distribution (N(1, 0.01) ). Up until now I managed to create the random number between 0.75 and 1.25, however, I am unsure how I can do it as a normal distribution (in other words, I want numbers closer to my original value to be shown more frequently).

This is the code I am using for the random number; however, as far as I understand, the probability that any number between 0.75 and 1.25 shows up is the same and does not follow a normal distribution:

{if(is_empty(wagerandom1),(rand(75,125)/100),wagerandom1)}

I am using an equation question type field.

Thank you in advance and best
Katharina 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 weeks 5 days ago - 3 weeks 5 days ago #266962 by Joffm
Replied by Joffm on topic Normal Distribution in Random Number
Hi,
unfortunately you did not mention if you are allowed to use javascript
and how do you want to use this "random number".

But you may read this
[url] en.m.wikipedia.org/wiki/Box%E2%80%93Muller_transform [/url]
as a background

and use this formula as equation
[url] stackoverflow.com/questions/6241784/gene...istribution-in-excel [/url]

Here's the formula in excel for a normal(0, 1) distribution:

Code:
=SQRT(-2*LN( RAND()))*COS(2 * PI()*RAND())

Then use this formula to scale your normal distribution to mean 10 and standard deviation of 7:
Norm(µ=b, σ=a) = a*Norm(µ=0, σ2=1) + b

This would make the equation in Excel:
Code:
=7* SQRT(-2*LN( RAND()))*COS(2 * PI()*RAND()) + 10


Just try by replacing the Excel function names by LimeSurvey function names.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 weeks 5 days ago by Joffm.

Please Log in to join the conversation.

  • KatharinaFietz
  • KatharinaFietz's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 weeks 5 days ago #266964 by KatharinaFietz
Replied by KatharinaFietz on topic Normal Distribution in Random Number
Hi Joffm,

Thank you and sorry about this. Yes, I can use Javascript and I want to use this number in the next question, where I will multiply the wage of a survey respondent by this randomly generated number.

I tried to translate the code in Excel to Javascript, however, I am think I am making mistakes.

I tried the following:

<script type="text/javascript">
    function generateNormalRandom(mean, stdDev) {
        let randomValue = mean + stdDev * (Math.sqrt(-2 * Math.log(Math.random())) * Math.cos(2 * Math.PI * Math.random()));
        return Math.max(0.75, Math.min(1.25, randomValue));
    }

       var randomValue = generateNormalRandom(1, 0.1)

</script>

Thank you very much in advance and best
Katharina

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 weeks 5 days ago #266965 by Joffm
Replied by Joffm on topic Normal Distribution in Random Number
Which type of question do you use to run the script.
Best is a question of type "short text".

And you neither store the result nor do you trigger the script.

So it could be
Code:
<script type="text/javascript" charset="utf-8">
 
$(document).on('ready pjax:scriptcomplete',function(){
 
  function generateNormalRandom(mean, stdDev) {
        let randomValue = mean + stdDev * (Math.sqrt(-2 * Math.log(Math.random())) * Math.cos(2 * Math.PI * Math.random()));
        return Math.max(0.75, Math.min(1.25, randomValue));
    }
 
       var randomValue = generateNormalRandom(1, 0.1)
 
      $('#question{QID} input[type="text"]').val(randomValue);
      $('#question{QID}').hide();  // or hide it by css class "d-none"
});
</script>
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • KatharinaFietz
  • KatharinaFietz's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 weeks 4 days ago #266974 by KatharinaFietz
Replied by KatharinaFietz on topic Normal Distribution in Random Number
That works! Thank you so much!I have one follow-up question (my apologies).I was planning to use the value generated by your code above and multiply it by the wage of a person (let's say 200).I called the question that generates the random number "wagerandom1."In the next question, I tried using: {round(wagerandom1 * 200)}, but the value displayed is always 0.Could you please indicate how I can use the random number in the next question?Apologies for all the questions, and thank you very much in advance!Best,
Katharina

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 weeks 4 days ago #266976 by Joffm
Replied by Joffm on topic Normal Distribution in Random Number
How do you display the survey?
Question by question?
Group by group?

Where is this equation?
On the same page as the script?
This can't work as the script starts on "document.ready", when everything is loaded.

Why don't you just send the lss export?

Joffm 
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • KatharinaFietz
  • KatharinaFietz's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 weeks 4 days ago - 3 weeks 4 days ago #266978 by KatharinaFietz
Replied by KatharinaFietz on topic Normal Distribution in Random Number
My apologies, here the answers to your questions and attached my lss export:

How do you display the survey? Group by group

Where is this equation? At the beginning of one group
On the same page as the script? Yes, seeing your comment on this, this is probably where my problem starts...

Thank you very much in advance and best!
Katharina
Last edit: 3 weeks 4 days ago by KatharinaFietz.

Please Log in to join the conversation.

  • KatharinaFietz
  • KatharinaFietz's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 weeks 4 days ago #266979 by KatharinaFietz
Replied by KatharinaFietz on topic Normal Distribution in Random Number
But I think if I move the wagerandom questions to a different question group it works, correct?
 

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose