Random decimal generation

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
I want to generate a random decimal for a variable, within a certain range. If I use "rand(0.5)" for example, it regards 0.5 as the minimum and will generate anything from that to "0.999999~infinity" or something like that.

But can I set a RANGE of variance? Like, 0.01 to 0.5, for example? If so, how?
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
A rand call with no arguments generates a number in the [0;1[ range. (Including 0, excluding 1)

In your example you can then use arithmetics to modify the range.

rand * (0.5-0.01) + 0.01
*hugs*
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
or you can also do

rand(50) * 0.01 + 0.01
 
Last edited by a moderator:

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
Thank you both!
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
For bonus points can you tell us the difference between my and Engr. Adiktuzmiko's code?
 

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
For bonus points can you tell us the difference between my and Engr. Adiktuzmiko's code?
Adiktuzmiko's code sets the random value from 1-50 then multiplies it by 0.01 to achieve the decimal.

Yours appears to, as you said, assign a random variable between 0 and 1, and set a multiplier between 0.5 and 0.01.

Is that right...?

EDIT: New question... is there any simple method to round up decimals? Because it looks really weird to display "5.99999999999998" etc...
 
Last edited by a moderator:

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
Bump.


Please see my EDIT in my previous post.
 

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
I tried "$game_variables[11].round" and then showed it in a message box, but it still wasn't rounded... what is the right way to do it?
 

Xypher

Veteran
Veteran
Joined
Apr 1, 2012
Messages
148
Reaction score
26
Primarily Uses
$game_variables[11] = $game_variables[11].round

if that doesn't work the number might be getting truncated at a prior calculation.
 

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
$game_variables[11] = $game_variables[11].round


if that doesn't work the number might be getting truncated at a prior calculation.
Thanks, it worked, but on second thought, this may not be what I want after all.


When displaying a variable in a text box with "\v[x]", is it possible to simply cut off the numbers after the decimal, while allowing them to still exist, just not be visible? If that makes sense.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
I think you can do .round(2) for example so it cuts off to 2 decimals

EDIT: Oh wait... I think that was for visual basic... 

Well, if your only concern is showing the decimals on text, you could always remove parts of the text once you convert that real number into a string

Like you can use 

String.index(".")

to find the index of the decimal place, then do

String[0,index+2]

to get a substring up to the decimal + 2 places

so you can do something like

text = $game_variables[number_index].to_s

$game_variables[text_index] = text[0,text.index(".") + 2]

I used two variables coz I assume you need the number value for something so it might be better to leave that variable alone
 
Last edited by a moderator:

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
If you only want two decimals do not use the code I gave you. Instead

or you can also do

rand(50) * 0.01 + 0.01
That was the main difference I wanted you to spot ^_^

*hugs*

 - Zeriab
 

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

Latest Threads

Latest Posts

Latest Profile Posts

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.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,045
Members
137,569
Latest member
Shtelsky
Top