Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Exeson · Feb 28, 2013 at 01:38 PM · rotaterandom.rangedistributionuniform

How to generate random values with a uniform distibution

Hey guys,

I'm currently a little stuck with something I want to do. I want to take an object that is having its rotation updated each frame and add some noise to that rotation.

Currently I am just using Random.Range(min float, max float) to generate three values in degrees that are combined into a vector. I then use that vector to rotate the game object in the Late Update function. My question is how can I ensure that each of the three random numbers I generate are uniformly distributed?

I have seen in the documentation for random there is Random.rotationUniform however I'm not sure if i can use that as I still want to be able to specify the minimum and maximum values.

Here is an example of the code I'm using:

 public GameObject rightShoulder;
 
 private float _a;
 private float _b;
 private float _c;
 
 private Vector3 _vectorA;
 
 void LateUpdate ()
 {
 _a = Random.Range(-45f, 45f);
 _b = Random.Range(-45f, 45f);
 _c = Random.Range(-45f, 45f);
 
 _vectorA = new Vector3 (_a, _b, _c);
 
 rightShoulder.Transform.Rotate (_vectorA, Space.Self);
 }
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikilo · Feb 28, 2013 at 02:06 PM 0
Share

Hi, I don't have an answer, but a question. What do you mean by uniformly distributed?

avatar image Exeson · Feb 28, 2013 at 04:04 PM 0
Share

http://en.wikipedia.org/wiki/Uniform_distribution_%28discrete%29

However I also need the mean of all the values generated over the time period be 0 or near zero.

avatar image Lockstep · Feb 28, 2013 at 04:20 PM 0
Share

That is not what it means. It really means, that all intervals of the same length have the same chance to be hit. Or in the discrete case: the chance of each outcome is 1/n where n is the number of possible outcomes.

On topic: Is this really not the case? Pseudo random numbers (computers can't generate real random numbers but nvm) are constructed to appear to be uniformly distributed. You can test this by adding those up:

 public long n = 10000000;
 private float test = 0;
 for(long i = 0 ; i < n ; i++){
     test += Random.Range(-45f,45f);
 }
 test /= n;
 Debug.Log("test is " + test);

Don't do this during Update() though.

avatar image Owen-Reynolds · Feb 28, 2013 at 05:07 PM 0
Share

$$anonymous$$ight help if you described the problem that code gives and what you want to happen.

Because your code is officially using a uniform distribution. It will probably tend to "walk" your rotation in a direction and not even try to recenter it. That's what uniform distributions do. Like if you roll three 6's in a row -- you aren't "due" for 1's.

avatar image _Yash_ · Jun 07, 2017 at 08:55 AM 0
Share

Use Random.rotationUniform to get the uniform rotation.

 Vector3 rot = Random.rotationUniform.eulerAngles;  // 0 - 360
 rot = rot * 0.25f;  // 0 - 90
 rot = rot - Vector3.one*45;  // -45 to 45

avatar image Owen-Reynolds _Yash_ · Jun 07, 2017 at 01:35 PM 0
Share

But this gives the same results as the original code (and takes a little bit longer to run and isn't as easy to tweak.) As you note, uniform rotation just rolls 0-360 three times. It's merely a typing shortcut for starting out an object with a completely arbitrary facing. It's not a special "rotation-y" way to roll dice.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Arrow, Spinning and rotating toward the tip 0 Answers

Roll cuboid does not work properly 1 Answer

How do I allow my player object to continue rotating once it's met a clamped boundary? 1 Answer

Wheel rotation 1 Answer

Constant Player Spinning to the left. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges