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

Get help using Construct 2

Post » Mon Jun 06, 2016 5:18 pm

Hello Everyone,

I am confusing, I need to Set a random value on a local variable inside a loop "For", but is not working.
Image
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
B
5
Posts: 8
Reputation: 256

Post » Mon Jun 06, 2016 5:39 pm

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?
B
19
S
6
G
2
Posts: 256
Reputation: 2,640

Post » Mon Jun 06, 2016 6:20 pm

Your loop should be from 0 to Array.Width-1 also.
ImageImageImage
B
71
S
21
G
221
Posts: 3,649
Reputation: 124,227

Post » Mon Jun 06, 2016 6:27 pm

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.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Mon Jun 06, 2016 9:22 pm

Josek5494 wrote: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][/i][/i][/i]


Hi Josek5494
Yes Great! The "floor" is exactly what I need, now it is working properly.
So many thanks!
B
5
Posts: 8
Reputation: 256

Post » Mon Jun 06, 2016 9:33 pm

blackhornet wrote: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).
B
5
Posts: 8
Reputation: 256

Post » Mon Jun 06, 2016 9:43 pm

Josek5494 wrote: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.
B
5
Posts: 8
Reputation: 256

Post » Mon Jun 06, 2016 9:47 pm

tenemide wrote: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...
B
19
S
6
G
2
Posts: 256
Reputation: 2,640

Post » Mon Jun 06, 2016 9:53 pm

"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.
ImageImageImage
B
71
S
21
G
221
Posts: 3,649
Reputation: 124,227

Post » Mon Jun 06, 2016 10:28 pm

blackhornet wrote:"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 :)
B
5
Posts: 8
Reputation: 256


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 12 guests