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 /
avatar image
0
Question by inejwstine · Aug 29, 2012 at 11:41 PM · randomrandom.rangerange

Is Random.Range really uniform?

I have a script randomly generating instances of different game objects. It uses Random.Range to decide which one to instantiate, but I'm not entirely convinced that it's returning a uniform distribution. I've been trying it with just two objects and have it count how many of each it spawns, and it seems to favor one object over the other. I just let it run, generating one random object per frame for several minutes, and the gap between the two numbers is now past 100 and steadily climbing (not quickly, but steadily). Every time I run it, I set the seed to be (int)Time.time, but it always creates more of the first object than the second one, no matter how many times I run it. Is there some trick to getting the distribution to be more uniform? Thanks a lot!

Comment
Add comment · Show 9
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 Piflik · Aug 29, 2012 at 11:49 PM 0
Share

There is a difference between random and uniform...while most humans would think the more uniform a distribution is, the more random it is, this is not true. If you want a uniform distribution, you would have to impose limits on the RNG to prevent bunched up results.

avatar image Fattie · Aug 30, 2012 at 06:01 AM 0
Share

Quite simply do NOT set the seed, leave it alone.

So, that's the answer. If that does not work, you have an unrelated bug, or, as Pfifik suggest you're just not used to seeing real random numbers.

avatar image Eric5h5 · Aug 30, 2012 at 06:21 AM 1
Share

If you're setting the seed to Time.time in Start or Awake, then it's always being set to 0, which explains why you get the same results every time.

avatar image inejwstine · Aug 31, 2012 at 10:26 PM 0
Share

Yes, I understand not every random distribution will be completely uniform; maybe I used the ter$$anonymous$$ology wrong, but I just meant that a large amount of random numbers should be more or less evenly distributed across the range in question (as opposed to, say, a normal distribution). And it did not appear to be evenly distributed, because like I said, the gap between the number of each value chosen kept growing. But anyway, removing the line where I set the seed seems to have helped. I'm not sure why ANY seed would have caused the behavior I was seeing though... most likely I'm still a bit ignorant about how random numbers actually work. ^^; Thanks for the help!

avatar image Eric5h5 · Aug 31, 2012 at 10:32 PM 2
Share

I'm not sure why ANY seed would have caused the behavior I was seeing though...

The whole point of setting the seed is to get the same results every time.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Aug 30, 2012 at 12:42 AM

There's no reason to set the seed, unless you specifically want a repeatable sequence. Unity uses Marsaglia's Xorshift 128 algorithm, which is high quality. This code demonstrates that there is no particular bias:

 var x = 0;
 var y = 0;
 for (var i = 0; i < 1000000; i++) {
     if (Random.Range(0, 2)) {
         x++;
     }
     else {
         y++;
     }
 }
 Debug.Log (x + " " + y);

If you run it a number of times, you'll find that x is greater than y approximately half the time, and both x and y always end up being fairly close to 500,000.

Comment
Add comment · Show 2 · Share
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 inejwstine · Oct 28, 2012 at 02:05 AM 0
Share

For some reason when I set the seed at the beginning, the results are NOT uniformly distributed, but when I remove the seed, it works fine. $$anonymous$$ysterious... Anyhow, thanks for the tip.

avatar image Paulius-Liekis · May 13, 2014 at 04:15 PM 1
Share

This doesn't prove uniform distribution. "Normal" distribution would also have same amount values around the average point, but it's not uniform.

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

11 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

Related Questions

[Beginner] Using Random.Range in initial game object position 1 Answer

How to know what random number is chosen 2 Answers

How can I Instantiate and do it in a random spot in a random spot? 2 Answers

How To Random Range a Float? 2 Answers

Store multiple random integers in an array? 4 Answers


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