0

Hey,

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";
                    break;
                case 1:
                    btn2.color = "#0000ff";
                    break;
                case 2:
                    btn3.color = "#00ffff";
                    break;
                case 3:
                    btn4.color = "#00ff00";
                    break;
            }
        }
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by ~s.o.s~
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.