# How do I Assign Array Values Randomly But With Purpose?

Get help using Construct 2

### » Mon Feb 29, 2016 11:07 pm

Hello and thanks for looking,

I wasn't sure how to word the topic, apologies for that. I'm trying to assign all the X values in an array to a random number between 0 and 4, which I did using 'For Each X Element--> Set Value At Array.CurX to int(Random(0,5))' This seems to work.

Now I need to figure out how to set some of them to 99 based on groupings I had set earlier. I.E. 'Array-->Set Value at int(random(2,5)) to 99, Set Value at int(random(5,8)) to 99, etc....' This also seems to work.

The hurdle has become, how do I now set an element within those same groupings to 5 without changing a value that was previously set to 99?

If this doesn't make any sense please, let me know and I'll try to clarify. Thank you for your time.

Admittedly, Im kind of an idiot when it comes to math and math concepts. Here is a graphic depicting what Im trying to do.

Last edited by gamecherry on Tue Mar 01, 2016 12:08 am, edited 1 time in total.
B
25
S
7
Posts: 79
Reputation: 2,267

### » Mon Feb 29, 2016 11:29 pm

It might be worth asking what are you using the array for ? Ive ran into trouble when I was using the wrong thing for a specific instance.
B
52
S
24
G
6
Posts: 391
Reputation: 7,855

### » Mon Feb 29, 2016 11:38 pm

Gearworkdragon wrote:It might be worth asking what are you using the array for ? Ive ran into trouble when I was using the wrong thing for a specific instance.

Thank you for your interest. I'm trying to populate a map with icons. The icon(s) are a single animation of 6 frames at 0 fps. I need the icons to be placed with a random frame 1-4. Within this map some of the icons need to be set at frame 5 (according to what group I assigned them to.) Some of them need to not exist, which is where the 99 value comes from (values of 99 are not instantiated onto the map.)

I want to set all X elements to a random number between 0 and 4. Then I want to set some elements to 99. Then I want to set some of them to 5 but not the ones I already to set 99.
I know, it's convoluted and obtuse.

I figured the best way to do this is to set up an array to hold the frame numbers for each icon so, I can save their location and frame number when exiting and entering the map.
B
25
S
7
Posts: 79
Reputation: 2,267

### » Tue Mar 01, 2016 4:58 am

Im an idiot I didnt read that clearly. yeah you gonna need one heck of a math wizard here cause what you want is basically some sort of algorithm. i dunno if they are still here lol
B
52
S
24
G
6
Posts: 391
Reputation: 7,855

### » Tue Mar 01, 2016 5:44 am

You could do this:
Code: Select all
`global number index =0+------------------------------+| while                        | set index to int(random(5,8))+------------------------------+   +---------------------------+   | array: value at index <99 | array: set at index to 5   |                           | stop loop   +---------------------------+`

Basically pick a random index until the value isn't 99 before setting the value.

You could probably do it all without an array though.
B
97
S
36
G
131
Posts: 5,514
Reputation: 83,466

### » Wed Mar 02, 2016 11:09 am

R0J0hound wrote:You could do this:
Code: Select all
`global number index =0+------------------------------+| while                        | set index to int(random(5,8))+------------------------------+   +---------------------------+   | array: value at index <99 | array: set at index to 5   |                           | stop loop   +---------------------------+`

Basically pick a random index until the value isn't 99 before setting the value.

Wow! Thank you so much R0J0hound. I incorporated that into my code and applied it to picking the 99s, the 5s, and some other things that I had yet to figure out when I posted and it works great. Those 2 lines of code relieved so much stress and Im grateful.
Thank you R0J0hound and everyone else for taking an interest in helping me.
B
25
S
7
Posts: 79
Reputation: 2,267