How do I Assign Array Values Randomly But With Purpose?

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

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

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

  • You could do this:

    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
       +---------------------------+[/code:3re0ismd]
    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.
  • You could do this:

    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
       +---------------------------+[/code:10wrpefu]
    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.

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