Check out the LimeSurvey source code on GitHub!

Generate Random Alpha numeric and store in question

More
3 years 2 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

Please Log in to join the conversation.

More
3 years 2 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.

Please Log in to join the conversation.

More
3 years 2 months ago #102354 by dweisser
Still at it. This is how I spend my Saturdays.

alert('test') works and everything. I'm beggin' here.

Please Log in to join the conversation.

More
3 years 2 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

Please Log in to join the conversation.

More
3 years 2 months ago - 3 years 2 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: 3 years 2 months ago by Ben_V.

Please Log in to join the conversation.

More
1 month 1 week ago #147370 by lunaluanlyy
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

Please Log in to join the conversation.

More
1 month 1 week 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.

Please Log in to join the conversation.

More
1 month 1 week 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 (or search sondages pro).
The following user(s) said Thank You: lunaluanlyy

Please Log in to join the conversation.

More
1 month 1 week ago #147507 by lunaluanlyy
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.

Please Log in to join the conversation.

More
1 month 1 week 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'm not a LimeSurvey GmbH member. I answer at the LimeSurvey forum in my spare time. No support via private message.
Some helpful links: Manual (EN) | Question Types | Workarounds
The following user(s) said Thank You: DenisChenu, lunaluanlyy

Please Log in to join the conversation.

More
1 month 1 week ago - 1 month 6 days ago #147519 by DenisChenu
Then finally :
{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:
{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 (or search sondages pro).
Last Edit: 1 month 6 days ago by DenisChenu. Reason: self} -> self)}

Please Log in to join the conversation.

More
1 month 1 week 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'm not a LimeSurvey GmbH member. I answer at the LimeSurvey forum in my spare time. No support via private message.
Some helpful links: Manual (EN) | Question Types | Workarounds

Please Log in to join the conversation.

More
1 month 1 week ago #147522 by holch
@Denis: tried both your codes, but LS is always complaining:


Haven't found the missing parentesis yet though...

I'm not a LimeSurvey GmbH member. I answer at the LimeSurvey forum in my spare time. No support via private message.
Some helpful links: Manual (EN) | Question Types | Workarounds
Attachments:

Please Log in to join the conversation.

More
1 month 6 days ago #147528 by DenisChenu
At end surely, i edited my post in some second.

You'r right about dweisser

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form