Random decimal generation

● ARCHIVED · READ-ONLY
Started by SoulCatapult 14 posts View original ↗
  1. 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?
  2. 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*
  3. or you can also do

    rand(50) * 0.01 + 0.01
  4. Thank you both!
  5. For bonus points can you tell us the difference between my and Engr. Adiktuzmiko's code?
  6. Zeriab said:
    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...
  7. Bump.


    Please see my EDIT in my previous post.
  8. .round
  9. 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?
  10. $game_variables[11] = $game_variables[11].round

    if that doesn't work the number might be getting truncated at a prior calculation.
  11. Xypher said:
    $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.
  12. 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
  13. Engr. Adiktuzmiko said:
    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! :D

    Thank you!! :thumbsup-left:
  14. If you only want two decimals do not use the code I gave you. Instead

    Engr. Adiktuzmiko said:
    or you can also do

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

    *hugs*

     - Zeriab