Forums

OverviewV-Play 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
    V-Play 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
    V-Play 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.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support