mercredi 16 mars 2016

How to use Math.random() for JSON random datas

I have a list of datas (flight) that I display in my HTML with a ng-repeat. I use Math.random() to get random numbers for some flight datas. You can find my expressJS code bellow :

    var price = Math.floor((Math.random() * 200) + 100);
    var timeDeparture = Math.floor((Math.random() * 20) + 1);
    var timeDestination = timeDeparture + 2;


    response = {
        "flights": [{
            "departure": departure,
                "destination": destination,
                "time": {
                    departure: timeDeparture,
                    destination: timeDestination
                },
                "price": price
        }, {
            "departure": departure,
                "destination": destination,
                "time": {
                    departure: timeDeparture,
                    destination: timeDestination
                },
                "price": price
        }, {
            "departure": departure,
                "destination": destination,
                "time": {
                    departure: timeDeparture,
                    destination: timeDestination
                },
                "price": price
        }]
    };

res.json(response);

With my code, I can have a random number for my variables, but my problem is that this just applies once to my variables, so all the random variables have the same value in each flight. To give an example of what I expect : For example with the variable price, I would like to have different randoms prices for each flight

Would you have an idea ? Thank you




Aucun commentaire:

Enregistrer un commentaire