STEP 1.3. Add the randomization code.
The following code performs all randomization for ten choices between randomly-generated sandwiches. We’ll
first examine how this code works, then how to customize it for other applications.
1 Qualt ric s . Sur veyE ngi ne . a ddOnload ( f unc tio n (){
2 // Set nu mber of choi ces ;
3 var numChoi ce = 5;
4
5 // Vec tors cont a ining all a ttri b ute l eve ls :
6 var brea dAr ray = [ " Bagel " , " Hero " , " Roll " , " S liced whit e ", " Tortilla " , " Lettuce wrap " ];
7 var chee seAr ray = [ " Che ddar ", " Gouda " , " Jack " , " Mo zzarell a " , " Pr ovolone " , " N on e " ];
8 var gree nAr ray = [ " Arugala " , " Green lettuce " , " Red l ett uce " , " Spina ch " ];
9 var meatArr ay = [ " Ham " , " Roast be ef " , " Tu rkey " , " Port obe llo " , " Egg " , " Bean patty " ];
10 var sauc eAr ray = [ " Hot sauce " , " M ayo nnai se " , " Mus tard ", " Oil and vin egar " , " None " ];
11 var vegg ieAr ray = [ " Tomato " , " J ala penos " , " Roasted pepp ers " , " On ion " , " Olives " , " B ean
sprouts " , " Pic kles ", " Avocad o " ];
12
13 // Fisher - Yates shu ffle :
14 function shuffle ( array ){
15 for ( var i = array . length - 1; i > 0; i - -) {
16 var j = Math. f loor ( Math . ra ndom () * (i + 1) ) ;
17 var temp = array [i ];
18 array [ i] = array [ j ];
19 array [ j] = temp ;
20 }
21 return array ;
22 }
23
24 // Shu ffle a vector , c hoos e the fi rst en try :
25 function shuff le_one ( theArray ) {
26 var out = shuffle ( theArray );
27 var out = out [0];
28 return ( out )
29 };
30
31 // Per form the r a ndomi z ation and sav e the re sult :
32 for ( i = 1; i <= num Cho ice ; i ++) {
33 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _br ead1 ", s huffle_ one ( breadAr ray ) ) ;
34 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _br ead2 ", s huffle_ one ( breadAr ray ) ) ;
35 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _ch ees e1 " , s huf fle_ one ( cheeseA rray ) );
36 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _ch ees e2 " , s huf fle_ one ( cheeseA rray ) );
37 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _gr een1 ", s huffle_ one ( greenAr ray ) ) ;
38 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _gr een2 ", s huffle_ one ( greenAr ray ) ) ;
39 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _meat 1 " , s huff le_ one ( mea tArray ) ) ;
40 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _meat 2 " , s huff le_ one ( mea tArray ) ) ;
41 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _sa uce1 ", s huffle_ one ( sauceAr ray ) ) ;
42 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _sa uce2 ", s huffle_ one ( sauceAr ray ) ) ;
43 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _ve ggi e1 " , s huf fle_ one ( veggieA rray ) );
44 Qualtri cs . S urv eyEn gine . setE mbed d edDa ta (" cho ice "+i+" _ve ggi e2 " , s huf fle_ one ( veggieA rray ) );
45 }
46 }) ;
Figure 4: Randomization JavaScript: fixed attribute order.
We want to randomly assign six attributes: bread, cheese, meat (and other hearty things one builds sand-
wiches around), greens, other vegetables, and sauce. The top of the code block, lines 4-9, creates a vector of
levels for each of these six attributes.
We want to randomly select exactly one of these levels for each sandwich. The middle of the code block
contains two functions for doing this. The first, shuffle(), shuffles the vector by implementing the Fisher-
Yates shuffle. The second, shuffle one(), is a wrapper that runs shuffle() and chooses only the first
element. For example, shuffle one(breadArray) chooses a random bread.
4