# Fill array with fixed percentage of objects

Get help using Construct 2

### » 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:

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
22
S
9
G
1
Posts: 788
Reputation: 3,788

### » Tue Nov 05, 2013 2:05 am

stil have some isues here

Dan
B
13
S
5
G
4
Posts: 176
Reputation: 4,429

### » Tue Nov 05, 2013 2:18 am

What do you wanna do? A chess board or a board where black and white fields are randomly distributed?

Anyway your mistake is: random(1) returns anything from 0 to 1 and a frame number like 0,123153151412 doesn't exist. If you want it to be either 0 or 1 you coud use the expression floor(random(0,2)).

However, if you only want to create a chess board, use something like this:

localvariable = 0
array for each element XY: array set at (array.curx, array.cury) = localvariable
create chesstile at x = array.curx*tilesizeinpixel and y = array.curx*tilesizeinpixel
- if localvarialbe = 0: set localvariable = 1
- else : set local variable = 0

This way you alternate 0 and 1 through your array, starting with 0. Additionally at each element the tile is created and framenumber is set.
If you create a chessboard like this, I suggest pasting all the tiles down to a canvas (with the canvas plugin) for better performance or just load an image of the board as tiled background instead of creating the tiles (you still can store information of pieces in your array).mindfaQ2013-11-05 02:24:29
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
22
S
9
G
1
Posts: 788
Reputation: 3,788

### » Tue Nov 05, 2013 2:32 am

ehh

i want to make a cheesboard with 2 or more individuel chess objects randomly created on the board

it must create 2 separate objects on same board and(not 2 Sprite frames or list animation : tokan at)

its almost like candy crush style

Dan istavang2013-11-06 00:44:10
B
13
S
5
G
4
Posts: 176
Reputation: 4,429

### » Sun Jun 08, 2014 2:24 pm

i have the same problem.... Cant spawn a random object from board width / board height....?
B
38
S
10
G
9
Posts: 542
Reputation: 9,181

Previous