I'm trying to get a random number for a small game which lights up random lights. I can't figure out a way to get the number random. I've tried seeding the Random() function with current time, but it's not working. It always lights up the third light.

Here's my code. It's the onTriggered part of a timer in QML.

onTriggered: {

            var now = new Date();
            var seed = now.getSeconds();
            var num = (Math.floor(4 * Math.random(seed)));
            switch(num % 4) {
                case 0:
                    btn1.color = "#ff0000";
                case 1:
                    btn2.color = "#0000ff";
                case 2:
                    btn3.color = "#00ffff";
                case 3:
                    btn4.color = "#00ff00";

There is no Math.random method which takes a seed. Simply use the Random class and its nextInt(upperBound) method and you should be good to go. Also, before lighting up the buttons, make sure the previous state of all the remaining buttons is reset.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.