Fill array with fixed percentage of objects

0 favourites
  • 14 posts
From the Asset Store
An educational game for Fill in the Blanks. An easy to use template for developers to build larger games
  • 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

  • Is the array to be filled a fixed size?

  • Is the array to be filled a fixed size?

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

    Tks

  • 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"

  • Second way -

    See Whiteclaw's post below ;)

  • You shall fill the array with "1" values and then you shall add a ramdomly positionned "2" in that same array 60 times ...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks guys, i'll try those solutions.

  • ehh

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

    cant get it to work

    ?? <img src="smileys/smiley5.gif" border="0" align="middle" />

  • 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

  • You shall fill the array with "1" values and then you shall add a ramdomly positionned "2" in that same array 60 times ...

    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 ->

    AABA

  • thanks for ur quick reply

    stil have some isues here <img src="https://gameportalen.com/array0.png" border="0" />

    Dan <img src="smileys/smiley1.gif" border="0" align="middle" />

  • 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

    set chesstile framenumber = localvariable

    • 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).

  • 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

  • i have the same problem.... Cant spawn a random object from board width / board height....?

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