How do I pick a random value in an array

Get help using Construct 2

Post » Fri Jul 22, 2016 2:37 pm

So i have and array that is made of 0 and 1, and I want to pick one random 1 value and turn it into 2. So how do you do that?
B
10
S
1
Posts: 18
Reputation: 611

Post » Fri Jul 22, 2016 3:00 pm

@Domse65

To be clear, your array has only 2 values? If so, an easy way is to use choose():
set at value at choose(0,1) to 2

Or you have several values of 0 and 1 and you only want to choose a random 1 value and turn it into 2?
B
17
S
9
G
5
Posts: 74
Reputation: 4,145

Post » Fri Jul 22, 2016 4:45 pm

SimoneT wrote:@Domse65

To be clear, your array has only 2 values? If so, an easy way is to use choose():
set at value at choose(0,1) to 2

Or you have several values of 0 and 1 and you only want to choose a random 1 value and turn it into 2?
I have several values of 0 and 1 and I only want to choose a random 1 value and turn it into 2
B
10
S
1
Posts: 18
Reputation: 611

Post » Fri Jul 22, 2016 5:59 pm

@Domse65

Got it!
This might not be the best way, but it's the best I have at this moment:

Create a second Array(Array2) to store the X location on your original Array where the value 1 might be found.
Then set value at Array2.At(floor(random(0, Array2.Width))) to 2.
Here is a capx to help:

https://www.dropbox.com/s/8x9qi6acmomaj ... .capx?dl=0
B
17
S
9
G
5
Posts: 74
Reputation: 4,145

Post » Fri Jul 22, 2016 6:37 pm

EDIT: Removed Link.

As mentioned furthur along in the thread, this method is actually pretty terrible in terms of efficiency and should not be used :p

Psuedocode:

While a 1 exists in the array, pick a random array cell between 0 and Array.Width.

Does the random cell contain a 1? If it does, add one to it and stop the loop.

Otherwise, repeat.


If you want to expand this to a 2d or 3d array, you'll use a similar method, but randomly pick XYZ coordinates instead. Not recommended because you will no longer be able to use IndexOf to confirm if the target value exists in the array, and can end up with an endless loop.
Last edited by oosyrag on Fri Jul 22, 2016 9:39 pm, edited 1 time in total.
Mistakes were made.
B
54
S
28
G
115
Posts: 1,705
Reputation: 65,083

Post » Fri Jul 22, 2016 7:26 pm

SimoneT wrote:@Domse65

Got it!
This might not be the best way, but it's the best I have at this moment:

Create a second Array(Array2) to store the X location on your original Array where the value 1 might be found.
Then set value at Array2.At(floor(random(0, Array2.Width))) to 2.
Here is a capx to help:

https://www.dropbox.com/s/8x9qi6acmomaj ... .capx?dl=0
Could you do an example with a two dimensional array? Because I need it for a two dimensional array
B
10
S
1
Posts: 18
Reputation: 611

Post » Fri Jul 22, 2016 8:53 pm

@Domse65

I used @oosyrag 's approach to solving the problem, but applied it to a 2d array:

https://www.dropbox.com/s/5orwwxtwyrvdm ... .capx?dl=0
B
17
S
9
G
5
Posts: 74
Reputation: 4,145

Post » Fri Jul 22, 2016 9:13 pm

@Domse65 Note:
The first code from SimoneT is the better and a more performant way -> save the positions of the nr. 1 in a new Array.
--> save the position in a 2d-array: first 'line' -> x-position in array secont-line -> y-position

Because worst case:
You have a large array but only 1 position for the right entry. It could take to a 'endless loop' because a random number be able occur multiple times.
... save the performance! 8-)
B
5
S
1
Posts: 24
Reputation: 423

Post » Fri Jul 22, 2016 9:59 pm

That makes a lot of sense. Here is the better one, for a two dimensional array.

https://www.dropbox.com/s/1cj0u7ipiioao ... .capx?dl=0
Mistakes were made.
B
54
S
28
G
115
Posts: 1,705
Reputation: 65,083


Return to How do I....?

Who is online

Users browsing this forum: dop2000, Yahoo [Bot] and 31 guests