# How do I generate a random number from a sequence?

Get help using Construct 2

### » 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
46
S
10
G
2
Posts: 149
Reputation: 4,338

### » Wed Jan 25, 2017 2:03 am

Similar idea here: viewtopic.php?f=147&t=186760
B
72
S
22
G
288
Posts: 3,862
Reputation: 158,460

### » 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
46
S
10
G
2
Posts: 149
Reputation: 4,338

### » 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.
B
72
S
22
G
288
Posts: 3,862
Reputation: 158,460

### » Wed Jan 25, 2017 8:59 am

And yes, of course, I always forget that random(a,b) includes a, but not b
B
46
S
10
G
2
Posts: 149
Reputation: 4,338

### » 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

Now you have a dictionary containing randoms with no duplicates.

How i did it before the plugin.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### Who is online

Users browsing this forum: SERGIOManOwaR and 4 guests