xChikyx

Regular
Regular
Joined
Aug 7, 2019
Messages
43
Reaction score
12
First Language
English
Primarily Uses
RMXP
Hi'm I'm currently developing a script,and i'm a total noob on this, but i came to a point where i just don't know what i'm doing wrong, and in theory it's very simple.

Lets say I need that the variable X gets a random number between 11 and 14 (including both ends)

how do i do that?
i tried x = rand(11..14) and it says it "can't convert Range into Integrer"
how should I write it then? ._.
 

Zerothedarklord

Regular
Regular
Joined
Jun 25, 2013
Messages
376
Reaction score
127
First Language
English
Primarily Uses
RMMV
have you tried x = rand(3) + 11?

I never did any scripting of any kind, nor did I ever use XP, so take my idea with a grain of salt.
 

estriole

Regular
Regular
Joined
Jun 27, 2012
Messages
1,537
Reaction score
766
First Language
indonesian
Hi'm I'm currently developing a script,and i'm a total noob on this, but i came to a point where i just don't know what i'm doing wrong, and in theory it's very simple.

Lets say I need that the variable X gets a random number between 11 and 14 (including both ends)

how do i do that?
i tried x = rand(11..14) and it says it "can't convert Range into Integrer"
how should I write it then? ._.
maybe rand method is not supported in XP version of ruby...
maybe try
Code:
(11..14).to_a.sample

or

[*11..14].sample
it will convert 11..14 range to array and then grab random member from that using .sample...

the second one might not works though since it might also not supported by xp ruby version too...
but it's worth a try :D.

but this method have downside though... if the range is too big... it might take quite some time to process... but what i mean too big is quite big range... ten thousand or above... and if you're only working with small number the performance won't be noticed.

hope this help...
 

MobiusXVI

Game Maker
Regular
Joined
Mar 20, 2013
Messages
404
Reaction score
104
First Language
English
Primarily Uses
So the "rand" function only supports returning a float value between 0 and 1 (exclusive) if you don't supply a value, or it returns integers in the range 0-N (exclusive) if you supply a value of N.

But that's ok! We can still use that to make lots of different kinds of random number generators. So what you want is a random number from 11 to 14 (inclusive). Ok, that means we want a random number that's 1 one of four possible values (11, 12, 13, 14). So we can start by generating a random number with four possible values using 'x = rand(4)'. This gives us either a 0, 1, 2, or 3. Then we just have to shift this to the desired range by adding the base (11) as 11+0=11, 11+1=12, etc.

Now we could do this manually each time, but it's a lot easier to just write a small method that does this for us, and then we don't have to worry about it.

Ruby:
def rand_range(start_range, end_range, include_end = false)
    range = end_range - start_range
    range += include_end ? 1 : 0
    return rand(range) + start_range
end
 

xChikyx

Regular
Regular
Joined
Aug 7, 2019
Messages
43
Reaction score
12
First Language
English
Primarily Uses
RMXP
So the "rand" function only supports returning a float value between 0 and 1 (exclusive) if you don't supply a value, or it returns integers in the range 0-N (exclusive) if you supply a value of N.

But that's ok! We can still use that to make lots of different kinds of random number generators. So what you want is a random number from 11 to 14 (inclusive). Ok, that means we want a random number that's 1 one of four possible values (11, 12, 13, 14). So we can start by generating a random number with four possible values using 'x = rand(4)'. This gives us either a 0, 1, 2, or 3. Then we just have to shift this to the desired range by adding the base (11) as 11+0=11, 11+1=12, etc.

Now we could do this manually each time, but it's a lot easier to just write a small method that does this for us, and then we don't have to worry about it.

Ruby:
def rand_range(start_range, end_range, include_end = false)
    range = end_range - start_range
    range += include_end ? 1 : 0
    return rand(range) + start_range
end

ohhhhh this is cool, i'll use this, thank you
 

Latest Threads

Latest Posts

Latest Profile Posts

Was real busy today but I found a few minutes to start on the named cultists and how to give each of them visually distinctive morphology within the confined of the cult robes. I really dislike straight recolors so I trying real hard with the small details on these.
Here's a BlueSkies 3 Announcement Trailer up! :yhappy: Hope you all enjoy! ^^ <3

Art Twitter is nice, Im so used to that platform being....A literal maw of despair.
ezgif.com-video-to-gif.gif

Forum statistics

Threads
134,834
Messages
1,251,049
Members
177,629
Latest member
tttttt
Top