Help me with very simple line of script RPGMXP

xChikyx

Veteran
Veteran
Joined
Aug 7, 2019
Messages
32
Reaction score
10
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

Veteran
Veteran
Joined
Jun 25, 2013
Messages
268
Reaction score
72
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

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,309
Reaction score
531
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
Veteran
Joined
Mar 20, 2013
Messages
383
Reaction score
91
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

Veteran
Veteran
Joined
Aug 7, 2019
Messages
32
Reaction score
10
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
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,090
Members
137,586
Latest member
Usagiis
Top