How would I implement this aloroghithm in LUA?

https://wikimedia.org/api/rest_v1/media/math/render/svg/46e09d833afb2466f517e90dbc0e363cb0bcdf8a
https://wikimedia.org/api/rest_v1/media/math/render/svg/5c1ef74f02625a0a670e7c7668e990d8446f8fde

/\

I have no idea where to start, help?

I’m trying to make a box muller aloroghithm so I can achieve nearly true randomness(maybe)
So how would I implement it?

There is a very helpful math library built in to lua which might help you.

I already knew that existed, but how can implement the “R”,“0 with a cross”,“U2”?

It’s non existent in math library.

It’s cool that you want to attempt this yourself. I just want to let you know that roblox has you covered on this one
https://developer.roblox.com/en-us/api-reference/datatype/Random

1 Like

I knew that exists, but I want it using an aloroghithm as random.new isn’t that random imo

Thanks for telling me, though.

How do you even measure an algorithm’s randomness? If this is just for fun then I think it’s good practice but if this is for a bigger project I wouldn’t focus on something as minor as this and just use the default random functions.

2 Likes

They want to achieve strong randomness as math.random() is based on time, this is not a safe option for some things.

If you want any form of true randomness generation within Roblox its most likely better to connect up to a server that can provide cryptographic/near true random.

However for this particular equation you might want to look at what other peoples solutions have done in other languages that can be mostly converted over to luau nicely. Here you can find a Box-Mueller random number generator in C# which you can convert over:

Here is another implementation but in C++:

Another thing to remember is that all random generation requires an input that is also random, so you may need to source that randomness from somewhere. A possible creative way to do this is to take all the users connected mouse movements and generate from that.

1 Like

Ye I do it ft

I’m not sure what you mean by this.

This states that it uses the “XSH-RR variant of the PCG family.”

You can even check out the source code for yourself

Theres no way to make ur own algorithm in lua, yet alone luau which removes features from lua. Your best bet is to make ur own c++ api which ur roblox lua sever sends a request to. U should use c++ as it’s a very powerful language so you can easily create your own algorithm in it.

Could always just use random.org through HTTPService, they let you use their API for free.

Hi,

The box-muller transform is used to take a uniform distribution and turn it into a normal distribution. In two dimensions, basically.

You would input 2 numbers, generated using Random.new(), which has a uniform distribution.

Then it would give you 2 numbers with a normal distribution.

To implement it, I think the math library should be enough. The Theta represents the angle, and R the radius, when you would write polar coordinates. But you can just ignore that part in the middle, and take the right and convert it into the left.

Calculating distributions will be very inefficient, as we cannot do vector operations on batches of numbers. But I guess for individual numbers its a fine solution.

So if you need only one normally distributed (with mean zero and variance 1) number, just take two numbers from Random.new() (between 0 and 1) and send them as U1 and U2 through either of the formulas, I think.

Disclaimer: I never used or implemented this so I may have gotten things wrong. X) There are also other ways to do the same.

Edit: Whoops, mistake, a normal distribution is unbounded. The resulting normal distribution has mean 0 and variance 1, so the output is not (neccessarily) in the range [0,1]. Corrected it.