# Random decimal generation

#### Solo

##### Veteran
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!
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*

##### Chemical Engineer, Game Developer, Using BlinkBoy'
or you can also do

rand(50) * 0.01 + 0.01

Last edited by a moderator:

Thank you both!

#### Zeriab

##### Huggins!
For bonus points can you tell us the difference between my and Engr. Adiktuzmiko's code?

#### Solo

##### Veteran
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
Bump.

Please see my EDIT in my previous post.

.round

#### Solo

##### Veteran
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
\$game_variables[11] = \$game_variables[11].round

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

#### Solo

##### Veteran
\$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.

##### Chemical Engineer, Game Developer, Using BlinkBoy'
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:

#### Solo

##### Veteran
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...
No, no, it works!

Thank you!!

#### Zeriab

##### Huggins!
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

### Latest Profile Posts

Did anyone else get a 'sever error' a little while ago on fourms? because I did...
So... today was a friend birthday and she explicitly requested this little scene, otherwise she'll come to my house and shot me with a water gun.

Kinda late to the party but if you're trying to make a game with relatively large roster, my suggestion: be ready for a long, long ride yourself.
were the servers acting weird a few minutes ago for anyone else or was it just me?
I finished playing Shadows of Adam. With just the increased stat monsters and keeping gear from completing game, I didn't feel much motivation to play new game plus. I feel that there should be new content to discover if new game plus is part of a game, like Chrono Trigger's multiple endings, or the Diablo series' vast collection of loot and lore items.