How do I randomly place numbers in an array?

0 favourites
From the Asset Store
Spooky Places game assets for 2D endless runner action adventure game.
  • I am wanting to create an array that has shuffled numbers in 6 different fields. I know how to choose a random number, but I don't know how to use that number only once.

    This is what I am trying to achieve.

    R = 4

    O = 1

    Y = 3

    G = 2

    B = 6

    V = 5

    R = 1

    O = 5

    Y = 4

    G = 6

    B = 2

    V = 3

  • You would need to keep track of each random number selected in its own variable and then check the new random number against each of those to make sure you are not getting repeated numbers.

    Looks like you need 6 variables and call them R1,R2 etc....

    Looks like Tom has a tutorial:

    https://www.scirra.com/tutorials/315/generating-random-numbers-without-duplicates/revisions/2089

  • Perfect! That is exactly what I needed!

    Thank you

  • The 6 variables are not the best solution (is not scalable). You can search directly in the array before setting a new random value with Array condition "Contains Value".

  • Perfect! That is exactly what I needed!

    Thank you

    You are very welcome!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I think Tom's tutorial will work the best for me.

    However, I am actually needing to randomly select from a specific set of numbers instead of 1-6. I need to use 0,2,4,6,8, and 10.

    The tutorial sets the range like this...

    System - Set RandomNumber to floor(Random(1,7))

    How can I randomly use my specific numbers instead of a range?

  • floor(Random(0,6)) * 2

  • I actually woke up this morning with a solution immediately in my head. I should have used CHOOSE instead of RANDOM in this case.

    I am now having difficulty with getting it to work with the number zero though...

    This works:

    floor(Random(1,6))*2

    or

    choose(2,4,6,8,10)

    But this does not:

    floor(Random(0,6))*2

    choose(0,2,4,6,8,10)

    The crazy thing is, when I make some changes to allow text instead of numbers it works.

    choose("0","2","4","6","8","10")

    Even though the numbers were entered via text, will this cause any problems? Should I just go with this?

  • I actually woke up this morning with a solution immediately in my head. I should have used CHOOSE instead of RANDOM in this case.

    I am now having difficulty with getting it to work with the number zero though...

    This works:

    floor(Random(1,6))*2

    or

    choose(2,4,6,8,10)

    But this does not:

    floor(Random(0,6))*2

    choose(0,2,4,6,8,10)

    The crazy thing is, when I make some changes to allow text instead of numbers it works.

    choose("0","2","4","6","8","10")

    Even though the numbers were entered via text, will this cause any problems? Should I just go with this?

    0 multiplied by 0 is 0

  • [quote:3io59dhr]0 multiplied by 0 is 0

    That's okay, I need a single 0 as one of the results.

    When the tutorial program is run, it locks up without displaying the results like normal. Same thing with choose(0,2,4,6,8,10). It will not give me a result. If i swap out the 0 like choose(1,2,4,6,8,10), it works, but no zero. I can get this to work though, choose("0","2","4","6","8","10").

  • [quote:3t2paafh]0 multiplied by 0 is 0

    That's okay, I need a single 0 as one of the results.

    When the tutorial program is run, it locks up without displaying the results like normal. Same thing with choose(0,2,4,6,8,10). It will not give me a result. If i swap out the 0 like choose(1,2,4,6,8,10), it works, but no zero. I can get this to work though, choose("0","2","4","6","8","10").

    That is because you are trying to multiply by zero. Change it to 1 through 11 and then subtract one from the result and you will get your chosen range.

  • Where am I multiplying zero by zero? The only multiply I can see is, floor(Random(0,6))*2.

    I can't get choose(0,2,4,6,8,10) to work either, I just want it to be able to pick the zero and give it to me.

  • Where am I multiplying zero by zero? The only multiply I can see is, floor(Random(0,6))*2.

    I can't get choose(0,2,4,6,8,10) to work either, I just want it to be able to pick the zero and give it to me.

    Random uses the first number in the range which you have as zero and you have 0*2 and you can't multiply 0.

    Change range to 1,8 and then subtract 1 from the result and it will give you range 0 to 6.

  • But this does not:

    floor(Random(0,6))*2

    choose(0,2,4,6,8,10)

    https://www.dropbox.com/s/c5h01uk2tybyc ... .capx?dl=0

    Both work fine ?

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