Randomising in JavaScript

Video

JavaScript Notes

JavaScript
    // Creating random numbers in JavaScript
    // Not "real" random numbers
    // don’t use this for security
    // Math.floor(Math.random( ) * (range + 1)) + minimum
    // Number line
    // 0-- 	1 	2 	3
    
    let num;
    let classes = ['highlight', 'active', 'sale']; 
    let people = ['John Bonham',
                'Robert Plant',
                'John Paul Jones', 
                'Jimmy Page'];
    let log = console.log;
    
    // Number between 1 and 3
    num = Math.floor(Math.random) * (2+1)) + 1;
     
    log(num);
    
    // Random person
    let max = people.length-1; // 4 – 1 = 3 
    let min = 0;
    num = Math.floor(Math.random) * (max - min +1)) + min; 
    log(people[num]);
    
    // Random CSS class
    let max2 = classes.length-1; // 4 – 1 = 3 
    let min2 = 0;
    num = Math.floor(Math.random) * (max2 - min2 +1)) + min2; 
    log(classes[num]);
    
    // Number between 500 and 1000
    num = Math.floor(Math.random) * (500+1)) + 500;