How do I generate a random number from a sequence?

Get help using Construct 2

Post » Wed Jan 25, 2017 1:48 am

How do I generate a random number with from a set sequence?

For example, if I randomly want to assign the X value of a sprite, every 10 pixels of a range of 0 - 70), how would I go about doing that?
Last edited by Sean Noonan on Wed Feb 01, 2017 4:16 pm, edited 1 time in total.
B
45
S
9
G
2
Posts: 141
Reputation: 4,197

Post » Wed Jan 25, 2017 2:03 am

Similar idea here: viewtopic.php?f=147&t=186760
ImageImageImage
B
71
S
21
G
231
Posts: 3,688
Reputation: 129,352

Post » Wed Jan 25, 2017 2:11 am

I worked it out... I came here because I thought I was missing something, but I solved it with some very simple maths...

To solve my given example...

int(random(0,70))*10

I think this is the simplest way to solve it, no? I'll make a little example project tomorrow if people find it useful.
B
45
S
9
G
2
Posts: 141
Reputation: 4,197

Post » Wed Jan 25, 2017 2:14 am

int(random(0,8))*10

You were multiplying by 10 twice. This will give 0 to 70, with possible duplicates.
ImageImageImage
B
71
S
21
G
231
Posts: 3,688
Reputation: 129,352

Post » Wed Jan 25, 2017 8:59 am

And yes, of course, I always forget that random(a,b) includes a, but not b :D
B
45
S
9
G
2
Posts: 141
Reputation: 4,197

Post » Wed Jan 25, 2017 9:49 am

If you dont want to use a 3th party plugin, you can always use a Dictionary.
(personaly i very much like the BHT Smart Random plugin)

Clear the Dictionary

While
Compare 2 values > Dictionary.KeyCount ... is less then the amount of numbers you want (i suppose 7)
Local variable 'r' / number
..........actions ... set r to floor(random(0,8))*10
...........................Dictionary add key .. key is zeropad(r,2) ... value = r

Now you have a dictionary containing randoms with no duplicates.

How i did it before the plugin.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950


Return to How do I....?

Who is online

Users browsing this forum: tarek2 and 19 guests