Fill array with fixed percentage of objects

Get help using Construct 2

Post » Mon Mar 18, 2013 4:46 pm

Hi, i just want to fill an array with 2 components at random positions, but i need 60% of componentA and 40% of componentB in the array, can anyone give me a hand with this?
Thanks
cesisco2013-03-18 19:50:01
B
33
S
11
G
9
Posts: 381
Reputation: 10,153

Post » Mon Mar 18, 2013 9:08 pm

Is the array to be filled a fixed size?
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Mon Mar 18, 2013 9:28 pm

[QUOTE=theubie] Is the array to be filled a fixed size?[/QUOTE]

Hi, yes, i don't know the size yet, maybe 40x40.

Tks
cesisco2013-03-18 21:30:22
B
33
S
11
G
9
Posts: 381
Reputation: 10,153

Post » Mon Mar 18, 2013 10:01 pm

Two ways -

First way won't give you exact distribution as a whole, but rather a 40/60% chance per array element.

Array for each element set instance variable random floor(random(10)
If random < 6, set element at CurX, CurY to "componentA"
If random >= 6, set element at CurX, CurY to "componentB"
oosyrag2013-03-18 23:17:46
Mistakes were made.
B
37
S
17
G
67
Posts: 1,041
Reputation: 38,211

Post » Mon Mar 18, 2013 10:03 pm

Second way -

See Whiteclaw's post below ;)oosyrag2013-03-18 23:17:31
Mistakes were made.
B
37
S
17
G
67
Posts: 1,041
Reputation: 38,211

Post » Mon Mar 18, 2013 10:22 pm

You shall fill the array with "1" values and then you shall add a ramdomly positionned "2" in that same array 60 times ...
B
35
S
16
G
16
Posts: 2,222
Reputation: 16,589

Post » Tue Mar 19, 2013 9:35 am

Thanks guys, i'll try those solutions.
B
33
S
11
G
9
Posts: 381
Reputation: 10,153

Post » Mon Nov 04, 2013 10:47 pm

ehh

could you help me whit a capx or screenshoot on this 50% array isue

cant get it to work

??
B
12
S
5
G
4
Posts: 176
Reputation: 4,384

Post » Mon Nov 04, 2013 11:18 pm

The way I'd do it is:

for each element:
local variable choice = 0
Set choice to random(1)
if choice < 0.6, set current value to component A
else set current value to component B
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Tue Nov 05, 2013 1:18 am

[QUOTE=Whiteclaws] You shall fill the array with "1" values and then you shall add a ramdomly positionned "2" in that same array 60 times ...[/QUOTE]

When filling with "2"s you would run into the problem that random filling would hit other already filled in "2"s in the process, so you'd have to loop some unnecessary times till you managed to find a free place for all "2".

Easier way that is random and still obtains a 40/60-distribution:

[your trigger]
chance = 0.4
elementsA = 0
elements = 0
- empty event : set elements = array.height*array.width*array.depth
set elementsA = elements * chance
- array for each xyz:
-- system compare: random(1) less or equal elementsA/elements: set at (array.curx, array.cury ,array.curz) = A
substract 1 from elementsA
substract 1 from elements
-- else: set at (array.curx, array.cury ,array.curz) = B
substract 1 from elements



examplatory runthrough what it does:

4 places: oooo
Let's sayA has a chance of 0.75. Elements is 4, so elementsA is 3 -> we get a chance of 3/4.

for first place the system rolls: rand(0) < 3/4 ->
Aooo
A's chance is now 2/3 (since elementsA was substracted by 1 as well as elements)
for the second place the system rolls a 0.54 ->
AAoo
A's chance is now 1/2
now the system rolls 0.8 ->
AABo
for the last roll the chance for A is 1/1 ->
AABAmindfaQ2013-11-05 01:21:35
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
20
S
9
G
1
Posts: 785
Reputation: 3,725

Next

Return to How do I....?

Who is online

Users browsing this forum: anty21ro, Magistross, PavleNikolic and 20 guests