How do I make a random local variable on a loop For ?

0 favourites
  • 10 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hello Everyone,

    I am confusing, I need to Set a random value on a local variable inside a loop "For", but is not working.

    https://www.dropbox.com/s/mwykbtzsagu3cgn/test.JPG?dl=0

    When I put a constant value or I add the value, it is working. but, when i want to use "random" it is the 0 value returned.

    I hope you can explain me what is the problem

    Best regards

  • Use floor(random(0,Dictionary.KeyCount)) or floor(int(random(0,Dictionary.KeyCount))) instead and see what happens.

    Are you sure the dictionary is well filled and the keyCount is returning a valid value?

  • Your loop should be from 0 to Array.Width-1 also.

  • And the "loto" variable is used before receiving its random value. You should reorder things a bit otherwise 0 will always be the first number.

  • Use floor(random(0,Dictionary.KeyCount)) or floor(int(random(0,Dictionary.KeyCount))) instead and see what happens.

    Are you sure the dictionary is well filled and the keyCount is returning a valid value?

    /i]

    Hi Josek5494

    Yes Great! The "floor" is exactly what I need, now it is working properly.

    So many thanks!

  • Your loop should be from 0 to Array.Width-1 also.

    Thank you for your comment. The Array.width is 1000

    If I use "Array.Width-1", I have 0 to 998 so 999 values in X (Debug layout). But in my case I want to have 1000 values exactly (0 to 999).

  • Use floor(random(0,Dictionary.KeyCount)) or floor(int(random(0,Dictionary.KeyCount))) instead and see what happens.

    Are you sure the dictionary is well filled and the keyCount is returning a valid value?

    Again,

    I want to understad Why "random" is not working without floor. My first think is because the "random" use decimal value, isn't it ?

    Thank you to help me to understand better my error.

  • Again,

    I want to understad Why "random" is not working without floor. My first think is because the "random" use decimal value, isn't it ?

    Thank you to help me to understand better my error.

    Yes, random returns a value, which can be decimal. For example: random(0,2) can return 0.01 , 0.02 , 0.5 , 1 , 1.09...

  • "For" is inclusive, so it will run from 0 to 1000 if you use 0 to Array.Width, that's why you need 0 to Array.Width-1.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • "For" is inclusive, so it will run from 0 to 1000 if you use 0 to Array.Width, that's why you need 0 to Array.Width-1.

    Hi Blachornet

    Yes you are right, I have trying with Array.Width-2 and the latest value is 0.

    Thank you very much to help me, master

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)