Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How can I ensure both randomly generated colors are not the same?
Forum Updated to NodeBB v4.3 + New Features

How can I ensure both randomly generated colors are not the same?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqt quick
3 Posts 3 Posters 477 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    newbiSoso
    wrote on last edited by
    #1

    I'm making a simple game where two shapes change colors repeatedly using a timer, I have an array of colors to choose from how can I make sure both shapes will have be colored differently?

    My code:

    property variant colorArray: ["#008499","#963A65","#01FF97","#FF4140"] //colors to choose from
    Timer{
            id: color_switch
            interval: 1000; running: true; repeat: true
            onTriggered: {
                shape1.color = colorArray[Math.floor(Math.random()*3)]
                shape2.color = colorArray[Math.floor(Math.random()*3)]
            }
        }
    
    JonBJ 1 Reply Last reply
    0
    • N newbiSoso

      I'm making a simple game where two shapes change colors repeatedly using a timer, I have an array of colors to choose from how can I make sure both shapes will have be colored differently?

      My code:

      property variant colorArray: ["#008499","#963A65","#01FF97","#FF4140"] //colors to choose from
      Timer{
              id: color_switch
              interval: 1000; running: true; repeat: true
              onTriggered: {
                  shape1.color = colorArray[Math.floor(Math.random()*3)]
                  shape2.color = colorArray[Math.floor(Math.random()*3)]
              }
          }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @newbisoso
      So in some shape or form you have to write a tiny bit of code there which checks: while the second call to Math.random() * 3 returns the same as the first call did, call it again!

      1 Reply Last reply
      2
      • IntruderExcluderI Offline
        IntruderExcluderI Offline
        IntruderExcluder
        wrote on last edited by
        #3

        Just genereate 2nd index until it is not same as 1st index:

                let idx1 = Math.floor(Math.random() * 3);
                let idx2 = Math.floor(Math.random() * 3);
                while (idx2 === idx1)
                    idx2 = Math.floor(Math.random() * 3);
        
        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved