Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Randomly Assign Field Value?

Avatar

Former Community Member
Hi I'm wondering if there is away to create a field that will randomly choose 1 of the 3 options in it.



We are having a company BBQ and I was hoping that the PDF could tell the person what to bring since its potluck style. Normally we do printed invites, mix them up (ones saying desserts others saying appetizer etc.) and hand them out.



This is my first time using LifeCycle so any suggestions would be appreciated!



...I should add that I'm using LifeCycle 8...



Thanks,



Jenna
8 Replies

Avatar

Level 7
Use JavaScript's Math.random() function to get a random number between 0 and 1.

Avatar

Former Community Member
Thank you!

I'm not sure how to do that but I will try to figure it out!



Thanks again!

Avatar

Former Community Member
Getting it to display a random number was pretty easy acutually but I'm having problems assigning values to the numbers though. I made an array giving the numbers values but as soon as I paste it in, it only picks the last one "an appetizer".



I'm unfortunatly not javascript handy at all. Any help you be greatly appreshated.



This is the little script I've pasted together:



Math.floor(Math.random()*3+1)



meal = new Array(3);

meal[1] = "a dessert";

meal[2] = "a salad";

meal[3] = "an appetizer";

Avatar

Former Community Member
I was given this script to try out but it doesn't work at all. Am I missing something?



meal = new Array(3);



meal[0] = "a dessert";

meal[1] = "a salad";

meal[2] = "an appetizer";



randnum = Math.floor ( Math.random() * meal.length );



document.write(meal[randnum]);



Thanks for the help!!!!

Avatar

Level 7
There are many variations of JavaScript and LiveCycle Designer has added other limitations.



For LiveCycle Designer try for the item field's calculation script try:



var randnum = Math.floor ( Math.random() * 3);



if(randum == 0)

$.rawValue = "a dessert";

if(randum == 1)

$.rawValue = "a salad";

if(randum == 2)

$.rawValue = "an appetizer";

Avatar

Former Community Member
I copied and pasted it in but it didn't work. Is there a certain "Show" I should be using? I have tried it on Enter, Initalize and Calculate.



Thanks for your help!!!!

Avatar

Former Community Member
Still can't seem to get this thing to work... I can get a random number but I can't get the value assigned to that random number.

Avatar

Former Community Member
Thanks, I'm not sure why its working now but I got to work:

var randnum = Math.floor(Math.random()*3)



if(randnum == 0)

$.rawValue = "a dessert";

if(randnum == 1)

$.rawValue = "a salad";

if(randnum == 2)

$.rawValue = "an appetizer";



Thanks!