We need: math.randomf(-1,1)

How about a math.random function that returns a random float in between given numbers.
Ex: math.randomf(-2, 6) = 2.2344

And YES, I know I can do:

(math.random()-0.5)*f
or
math.random()*f - f/2
or
math.random() + math.random(1, 99)
or any other ugly coding.

I use these daily and I think it’d be much appriciated to add a random float value function.

1 Like
local math = setmetatable({
randomf = function(a,b)
return math.random() * (b-a) + a
end
},{__index=math})

Also ugly code?

Also, in your examples, what is f?

1 Like

Feature requests that use this reason should be rejected automatically.

7 Likes

But I want a method in Workspace called MakeFrontPageGame which I can call to make my code beautiful.

1 Like

Hold on, why would ugly coding matter?
Do you collaborate between different script’ers and thus it is why it is important to have proper easily understandable formatting?
Otherwise, only you’ll be the only one able to see the code.

But to reply to OP’s post, I think it’s not a bad feature to have if there’s another variable that helps with math.random code. I also use math.random and having shortcuts is always good to go for these things. It’ll make little room for error.

math.random(1, 10) / 10 Works fine.
I don’t see why we’d need this.

1 Like

Probably “float”

That’s not really the feature request, he wants a random function that returns a random float between a and b, instead of an integer. Besides, your code example has limited significance (only 1 decimal).

Disregarding all reasons given, I will say that it would be nice to have a C side implementation of this, if only for performance reasons.

Then again, the performance hit from doing this in Lua isn’t so bad.

From Lua’s official reference manual:

function math.randomfloat( min, max )
        return min + (max-min) * math.random()
end
2 Likes

I don’t think RBXLua has that.

Neither does regular lua…? This is just a function you can put in your code to use

1 Like

Why?
Writing same function over and over in different scripts IS ugly and bad coding.

Which is why you can use a function or module. Write it once and point to it in all scripts that require it. Then you’ll never have to think about it again.

This request is something that’s really easy to make by yourself and I don’t think it’s worth the effort to add it.

Edit: I thought I replied to PlaceRebuilder specifically. Apparently I didn’t. I really have to get used to this new software…

Yes, making a global function or make a module, using require in every script you want to use the simple function, that’s what I ment feels unnessecary.

Doing “Utils.randomf” or “math.randomf” is almost no difference.
The only difference is at the top: local Utils = require(1234)
(Using an assetId because that’s the easiest, but not necessary)

Nice! I just thought it was a function we all uses rather frequently and thus it was a good idea to add.

The whole reason Lua exists is because people needed a compact multi-platform language.
Why it is so compact is because it lacks tons of useful functions, such as math.randomfloat

Roblox could surely add a lot to it, including this, but I think they want to keep it as compact as possible.

Try ModuleScripts. They’re useful.

You do this in every programming language though. At the top of every single one of your files, you have lines and lines of just imports. “import java.awt.Color;” is hardly different from “require(game.ReplicatedStorage.Color)” Sure, game.ReplicatedStorage is a bit annoying to type out, but once you’re done typing it, it’s no different from “import java.awt.Color;”.

I guess it is kind of ugly though. It would be neat if ROBLOX could detect a block of requires at the top of your script as just importing stuff, allow you to collapse it, and then maybe create a window you could dock that had a list of all of the variable names of the things you imported for quick reference.

1 Like

Basicly declare the locals on a line, the requires in a do-block and collapse the do-block?