Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 1 Support › How can I generate random Integer numbers using "Utils"

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #5899

    studio scroll

    Hello, I want to generate random integer numbers using utils.generateRandomValueBetween(min,max), but I am getting floating points numbers only.

    The idea is to display the numbers on the entities when they are created.

    #5900

    Christian
    Felgo Team

    Hi,

    you can use Math.round() to get an integer from a float value.

    Cheers, Chris

    #5901

    studio scroll

    Ok, thanks. I got integer numbers by using:

    property int randNums:utils.generateRandomValueBetween(1,100);

     

    —-

    //and then, passing the value from randNums to Text.

    Text {
    id:infoText;
    width:200;
    height:40;
    text: randNums
    }

    However, it wasn’t exactly what I was looking for,

    I needed unique numbers, but I got repeated numbers, so I guess I have to create a function to achieve that. Otherwise

    if there is a function in VPlay that I can use to generate unique random numbers, kindly let me know. Cheers!

    #5902

    David

    Hi,

     

    You can simply use embedded Javascript functions to generate random integers. Following code gives you a random number between 0 and 1000.

    var upperRange = 1000
    Math.floor((Math.random()*upperRange)+1)

     

    Cheers,

    David

    #5904

    Christian
    Felgo Team

    The function utils.generateRandomValueBetween(min, max) does generate a new random number every time you call it. If you assign it to a property, then it only gets evaluated once at instantiation. However, the following example works as expected – each time a new entity is generated it gets assigned with a new integer value:

    import VPlay 1.0
    import QtQuick 1.1
    
    GameWindow {  
    
      EntityManager {
        id: entityManager
        entityContainer: scene
      }
    
      Scene {
        id: scene
    
        Component.onCompleted: {
    
          for(var i=0; i<10; i++) {
            entityManager.createEntityFromComponentWithProperties(entityComponent, {x: i*20})
          }
        }
    
    
      }
    
      Component {
        id: entityComponent
    
        EntityBase {
    
          property int randNums: utils.generateRandomValueBetween(1,100)
    
          Text {
            text: randNums
            color: "blue"
          }
        }
      }
    }
    
    

     

    Does this solution work for you?

    Cheers, Chris

Viewing 5 posts - 1 through 5 (of 5 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded