# Generate Random Alpha numeric and store in question

3 years 10 months ago #102348 by dweisser
dweisser created the topic: Generate Random Alpha numeric and store in question
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)

As always, any assistance is appreciated.
David

3 years 10 months ago #102352 by dweisser
dweisser replied the topic: Generate Random Alpha numeric and store in question
I'm still at it - no avail! I've tried many snippets like this.

<script>
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
});
</script>

Anyone have thoughts on why this doesn't work? the question type is short text.

3 years 10 months ago #102354 by dweisser
dweisser replied the topic: Generate Random Alpha numeric and store in question
Still at it. This is how I spend my Saturdays.

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

3 years 10 months ago #102356 by dweisser
dweisser replied the topic: Generate Random Alpha numeric and store in question
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.
The following user(s) said Thank You: DenisChenu, Ben_V

3 years 10 months ago - 3 years 10 months ago #102363 by Ben_V
Ben_V replied the topic: Generate Random Alpha numeric and store in question
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 10 months ago by Ben_V.

9 months 2 days ago #147370
lunaluanlyy replied the 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

9 months 1 day ago #147404 by tpartner
tpartner replied the topic: Generate Random Alpha numeric and store in question
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.

9 months 1 day ago #147447
DenisChenu replied the topic: Generate Random Alpha numeric and store in question
@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).
An error happen ? Before make a new topic : remind the Debug mode .
The following user(s) said Thank You: lunaluanlyy

9 months 17 hours ago #147507
lunaluanlyy replied the 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.

9 months 15 hours ago #147514 by holch
holch replied the topic: Generate Random Alpha numeric and store in question
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.
The following user(s) said Thank You: DenisChenu, lunaluanlyy

9 months 14 hours ago - 8 months 4 weeks ago #147519
DenisChenu replied the topic: Generate Random Alpha numeric and store in question
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).
An error happen ? Before make a new topic : remind the Debug mode .
Last Edit: 8 months 4 weeks ago by DenisChenu. Reason: self} -> self)}

9 months 14 hours ago #147520 by holch
holch replied the topic: Generate Random Alpha numeric and store in question
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.

9 months 13 hours ago #147522 by holch
holch replied the topic: Generate Random Alpha numeric and store in question
@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.
##### Attachments:

8 months 4 weeks ago #147528
DenisChenu replied the topic: Generate Random Alpha numeric and store in question
At end surely, i edited my post in some second.