- Posts: 115
- Thank you received: 4
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
- Topic Author
- Offline
- Premium Member
Less
More
10 years 5 months ago #102348
by dweisser
Generate Random Alpha numeric and store in question was created 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
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
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 115
- Thank you received: 4
10 years 5 months ago #102352
by dweisser
Replied by dweisser on topic Generate Random Alpha numeric and store in question
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.
<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
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 115
- Thank you received: 4
10 years 5 months ago #102354
by dweisser
Replied by dweisser on 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.
alert('test') works and everything. I'm beggin' here.
The topic has been locked.
- dweisser
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 115
- Thank you received: 4
10 years 5 months ago #102356
by dweisser
Replied by dweisser on 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.
Nebraska David
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.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1128
- Thank you received: 329
10 years 5 months ago - 10 years 5 months ago #102363
by Ben_V
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)
Replied by Ben_V on topic Generate Random Alpha numeric and store in question
thanks for sharing..
=>related previous thread: Generate Unique PIN for user
=>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 5 months ago by Ben_V.
The topic has been locked.
- lunaluanlyy
- Offline
- New Member
Less
More
- Posts: 2
- Thank you received: 0
7 years 3 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
Thanks
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10119
- Thank you received: 3598
7 years 3 months ago #147404
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on 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.
The topic has been locked.
- DenisChenu
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13704
- Thank you received: 2502
7 years 3 months ago #147447
by DenisChenu
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.
Replied by DenisChenu on 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) ?
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.
- lunaluanlyy
- Offline
- New Member
Less
More
- Posts: 2
- Thank you received: 0
7 years 3 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
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11702
- Thank you received: 2755
7 years 3 months ago #147514
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on 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 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
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13704
- Thank you received: 2502
7 years 3 months ago - 7 years 3 months ago #147519
by DenisChenu
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.
Replied by DenisChenu on topic Generate Random Alpha numeric and store in question
Then finally :
But we can write like this:
@dweisser : if you found some time for our workaround in manual ? Or a LS EM example file
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 3 months ago by DenisChenu. Reason: self} -> self)}
The following user(s) said Thank You: prwwww
The topic has been locked.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11702
- Thank you received: 2755
7 years 3 months ago #147520
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on 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 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.