# What is the fastest way to generate a CFrame of random orientation?

Given:

``````local Rand = Random.new(SomeSeed)
``````

Would this be faster:

``````local Frame = CFrame.Angles(Rand:NextNumber(-math.pi, math.pi), Rand:NextNumber(-math.pi, math.pi), Rand:NextNumber(-math.pi, math.pi))
``````

Or this:

``````local Frame = CFrame.lookAt(Vector3.zero, Rand:NextUnitVector())
``````

Or some unbeknownst to me 3rd way (please share)?

As a side note, do you know if `Rand:NextUnitVector()` or `Vector3.new(math.random()-0.5, math.random()-0.5, math.random()-0.5)*2` is faster, even though the former is more accurate?

1 Like

I feel like the first would be faster. However, Iâ€™m ready to be proven wrong.

1 Like

Probably the first one as a guess.

Small Thing:

You can use `math.random()` without any arguments which will generate a random number with decimals. While being the â€śfastestâ€ť, It is however limited to numbers below `1`, which would have an Angle Below `60` in Radians.

There is a niche reason I am using `Random` instead of `math.random()`. Visual effects, such as a custom explosion, should be handled on the client only. So I use `FireAllClients()` from a `RemoteEvent`. But I want all clients to see exactly the same thing, even though no one would probably notice or care. So I pass a seed from the server with which the clients generate identical visual effects.

I am not Suggesting that you should use `math.random`, Iâ€™m saying that you could, but you are limited to a certain amount

(I mean, I am, and Im not)

`math.randomseed` exists though. However, I donâ€™t know if itâ€™s faster.

Oh, so I could just call `math.randomseed()` with the seed sent by the server in `OnClientEvent`, instead of using `Random.new`, would that work as expected?

1 Like

Itâ€™s really important in programming to be able to run these sorts of tests yourself. The odds are slim that anyone here will have tested it already. Occasionally youâ€™ll get a CS major who chimes in with what sounds like an educated response, but often as not theyâ€™re wrong for whatever reason such as oddities with how Lua and C interact.

Iâ€™m not sure if this is what you mean or not, but Iâ€™m going to say it anyways because your wording is a little vague and future readers may benefit from the knowledge. `NextUnitVector` exists to avoid the bias towards the poles that the first method creates.

CFrame.Angles method

NextUnitVector method

Iâ€™ll go run some benchmarks since Iâ€™m already in Studio, but I do want to re-emphasize how important it is to be able to run benchmarks yourself. Youâ€™d be done within 5 minutes instead of waiting an hour for some guesses and speculations.

Here are the results from a benchmark with 10,000 iterations each.

Stick with the NextUnitVector approach.

4 Likes

What if they were combined? For example, it used `CFrame.Angles` but it used the unit vector numbers converted to radians.

Youâ€™d run into bias towards the poles again.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.