Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Generating random parts on the surface of a sphere terrain, help?

Hi,

So I made a script that takes parts, positions them at the sphere's origin, rotates to a random degree angle and then go forwards by the sphere's radius. This makes the parts go to a random position on the sphere's surface.

It seems to be working just fine, but there's an issue though. The parts are more likely to appear on the top and bottom of the sphere. When I randomly position 20 parts, a majority of them end up on the top and bottom. I must have not done the math efficiently, so can anyone make it where it doesn't do that, and scatters the parts a little better?

part.CFrame = workspace.PlanetaryGravityB.Moon.CFrame
*CFrame.Angles(
math.rad(math.random(0,360)),
math.rad(math.random(0,360)),
math.rad(math.random(0,360)))
*CFrame.new(0,0,math.abs(diameter/2))

Any comments/answers will help. ~SkeletalReality

1 answer

Your script and your math are perfectly fine. The reason you are seeing the same angles repeated multiple times is because believe it or not, the math.random() function is not completely random.

How math.random() works exactly is that whenever you run the function, the script takes information from what is known as a "seed". A seed is a number that represents data–in this case, a sequence of numbers. If you set the seed the script picks number from as the same every time you run the game, you will get the same numbers returned every time. Whenever you test run a game in ROBLOX, math.random() picks from the same seed every time.

A way to get around this is to use the function math.randomseed(). Whenever this function is run, the seed for the random number generator is set to whatever number is inside of the parentheses. What you can do is put the function tick() as the parameter for the math.randomseed() function. tick() returns the amount of time the game has been running in seconds. After running math.randomseed(), the numbers generated by math.random() will be different, because it is taking the number sequence from a different seed.

Going back to your code, a way to make the bricks appear at a different location every time is to use the function math.randomseed() before you use the function math.random().

math.randomseed(tick()) --This will set the seed for random number generation to the amount of seconds the game has been running (which will usually be different every time, since tick() is very exact).

part.CFrame = workspace.PlanetaryGravityB.Moon.CFrame
*CFrame.Angles(
math.rad(math.random(0,360)),
math.rad(math.random(0,360)),
math.rad(math.random(0,360)))
*CFrame.new(0,0,math.abs(diameter/2))

If you are still curious, there is actually a Wiki page on the ROBLOX Wiki that explains how random numbers work. You can visit that website by clicking here.

EDIT: I realize my explanation for seeds may not have been the best, so I'll provide you with an example. If you've ever played Minecraft, you may have heard of a "seed". In Minecraft's situation, a seed's data represents exactly what blocks you will get in your world. If you put the same seed in for every time you generate a world, you will get the same world every time. That's why when you randomize the seed when generating a world, you get a different world every time. The same seed will produce the same data every time.

1 comment

0

Thank you for the answer, but the parts are still mainly scattering on the tops and bottoms of the sphere, no matter how many times I reset the seed. Here is a reference image: https://pbs.twimg.com/media/DFAMvwCUQAEgKJE.jpg So there is definitely a flaw in my math that causes it to do this, and I'm looking for a more efficient way.

This discussion is preserved as it was. New answers, comments, and votes are closed.