Welcome to the LimeSurvey Community Forum

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

Generate Random Alpha numeric and store in question

  • dweisser
  • dweisser's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
10 years 3 months ago #102348 by dweisser
I know this can be done - I just don't know how to do it.

Anyone have a simple implementation for storing and displaying the results of something like this:

Math.random().toString(36).slice(2)

in a question's answer?

As always, any assistance is appreciated.
David
The topic has been locked.
  • dweisser
  • dweisser's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
10 years 3 months ago #102352 by dweisser
I'm still at it - no avail! I've tried many snippets like this.

<script>
$(document).ready(
function(){
// Find a random number (here between 1 and 4)
var randNumber = Math.floor(Math.random()*4 + 1);
// Save the number as answer of this question
$('input#answer527453X176X1255').val(randNumber);
});
</script>

Anyone have thoughts on why this doesn't work? the question type is short text.
The topic has been locked.
  • dweisser
  • dweisser's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
10 years 3 months ago #102354 by dweisser
Still at it. This is how I spend my Saturdays.

alert('test') works and everything. I'm beggin' here.
The topic has been locked.
  • dweisser
  • dweisser's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
10 years 3 months ago #102356 by dweisser
So, just in case it helps anyone. This took forever, and it's somewhat of a workaround, but it gave me the desired result. No javascript - all expression manager.

I divided:
{floor(rand(100000,999999))} by
{time()} and rounded up to get my random number.

In:
{ceil(time()/floor(rand(1000,9999)))}

Then I used {substr("abcdefghijklmnopqrstuvwxyz", -rand(1,26),1)} to get random alpha characters.

The final implementation, in which I cleverly removed "i", "l" "o", and "O" can be seen here:
{join(ceil(time()/floor(rand(1000,9999))), substr("abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", -rand(1,48),1), substr("abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", -rand(1,48),1))}

Placing this in the source of your survey item will yield something like this:
231387Cp


Enjoy.
Nebraska David
The following user(s) said Thank You: DenisChenu, Ben_V
The topic has been locked.
More
10 years 3 months ago - 10 years 3 months ago #102363 by Ben_V
thanks for sharing..
=>related previous thread: Generate Unique PIN for user ;)

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Last edit: 10 years 3 months ago by Ben_V.
The topic has been locked.
More
7 years 2 months ago #147370 by lunaluanlyy
Replied by lunaluanlyy on topic Generate Random Alpha numeric and store in question
So how can I store it? I want to recall the same alphanumerical pin in my email template. The number got replaced if I use INSERTANS.

Thanks
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 2 months ago #147404 by tpartner
Place it in an equation type question and then reference the question code in the email - {Qcode} - manual.limesurvey.org/Expression_Manager#Access_to_Variables

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
7 years 2 months ago #147447 by DenisChenu
@David/dweisser : great thanks :) and great code here :)

you didn't add a is_empty test ? To do the number only one time (based on time) ?

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 following user(s) said Thank You: lunaluanlyy
The topic has been locked.
More
7 years 2 months ago #147507 by lunaluanlyy
Replied by lunaluanlyy on topic Generate Random Alpha numeric and store in question
Thanks I tried. In the email, I got the same number using INSERTANS and {Qcode}. But it is not the same one in the survey question.
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 2 months ago #147514 by holch
As Denis says, you probably need to add a "is_empty test". Because the random numbers are triggered everytime the question is called if you don't do it. So first check if the question is empty, then generate the code. This means the code will only be generated once and when you access the code via EM it will always be the same.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The following user(s) said Thank You: DenisChenu, lunaluanlyy
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 2 months ago - 7 years 2 months ago #147519 by DenisChenu
Then finally :
Code:
{if(is_empty(self),join(ceil(time()/floor(rand(1000,9999))), substr("abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", -rand(1,48),1), substr("abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", -rand(1,48),1)),self)}

But we can write like this:
Code:
{if(is_empty(self),
  join(ceil(time()/floor(rand(1000,9999))), substr("abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", -rand(1,48),1), substr("abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", -rand(1,48),1)),
  self)}

@dweisser : if you found some time for our workaround in manual ? Or a LS EM example file ;)

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.
Last edit: 7 years 2 months ago by DenisChenu. Reason: self} -> self)}
The following user(s) said Thank You: prwwww
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 2 months ago #147520 by holch
dweisser was logged into this forum for the last time 7 months ago. Chances that he is very active with limesurvey isn't too big I guess.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose