fill array with random

Get help using Construct 2

Post » Thu Jan 26, 2012 1:42 pm

Hello.
How i can fill array(10,1,1) with 10 results of random(1,10) function on start of layout?

Thanks in advance.
B
6
G
1
Posts: 14
Reputation: 1,353

Post » Thu Jan 26, 2012 2:02 pm

I know there has to be a cleaner way to do this, and I could in Python in a single line, but I'm not sure how to simply write code in Construct. Regardless, this takes 1 event and some screen space. You could use the variables however you want, but this gives them the random amount at start of layout.

Example capx

{Edit:} Just refresh your browser when testing the layout to see the variable numbers change and you'll see that it works. I had to use int(random(1,10)) because it was giving me floats without the int() code, and you didn't ask how to get to the ten-thousandths of a random number between 1 and 10.

Figured I'd post an example in case someone sees it and knows a better way, then we can both clean up our code a bit. Dark Savant2012-01-26 14:06:41
B
6
Posts: 10
Reputation: 445

Post » Thu Jan 26, 2012 2:11 pm

Thanks Dark, but i think that it can be done with construction like bellow

System - On start of layout   Array - Set Variable to floor(random(1,10))
- Array for each X element

System - Every 1.0 sec        Text - Set text to ""&Array.Number
- Array for each X element

B
6
G
1
Posts: 14
Reputation: 1,353

Post » Thu Jan 26, 2012 2:13 pm

Arg of course there's an easier way
ArrayRandomValues.capx
Last edited by Yann on Sun Sep 14, 2014 9:49 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu Jan 26, 2012 2:28 pm

Thnaks Yann
B
6
G
1
Posts: 14
Reputation: 1,353

Post » Thu Jan 26, 2012 9:19 pm

I knew there had to be a way to use "For" but didn't know how to get it in Constructor, thanks Yann.

I have a question though. Since floor() always rounds down, is it possible to randomly get a result of 10 unless you miraculously random a 10.0000000? I noticed on my example I wasn't getting 10 either, but that's why I used int() instead of floor(). This question is also one reason why I participated.
B
6
Posts: 10
Reputation: 445

Post » Thu Jan 26, 2012 9:45 pm

@Dark Savant: If you want to get 10 as a result of a random function, make it : random(11)
This will return a value from 0 to 10.
The last figure in a random() never gets reached.Kyatric2012-01-26 21:45:58
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Thu Jan 26, 2012 11:47 pm

Thank you! I was searching for such array example too.
Another question ,Is this possible to make each value unique ?
B
21
S
5
G
5
Posts: 163
Reputation: 5,028


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 14 guests