• 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
2
Question by TommyZakh · Sep 27, 2015 at 05:15 PM · randomrandom.rangestart

Random.Range always generates the exact same numbers in Start()

          float n1 = Random.value;
          float n2 = Random.Range(0f, 100f);
          int n3 = Random.Range(0, 100);
  
          Debug.Log(n1 + " " + n2 + " " + n3);

If I put this into the Start() function of an object, the exact same series of values will be generated every single time I run the game. I have no clue how to fix this absurd problem, so does anyone have any ideas?

Comment
Add comment · Show 3
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 TommyZakh · Sep 26, 2015 at 11:19 PM 0
Share

No, wait, it didn't, its still broken.

avatar image tanoshimi · Sep 27, 2015 at 05:37 PM 0
Share

Yeah, that's pretty much by design....

avatar image TommyZakh tanoshimi · Sep 28, 2015 at 06:38 AM 0
Share

Gotcha. All fixed.

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Arycama · Sep 28, 2015 at 06:24 AM

Before calling your Random functions, set the Random.Seed to a number that isn't likely to be the same twice in a row. An easy way to do this would be:

 Random.seed = System.DateTime.Now.Millisecond;

Add this line above your existing code (Before you call Random.value etc), and it will make the values seem much more random, as they would only be the same if you could go back in time and run the program again at the exact same millisecond as before. ;)

Think of a random number generator on a computer as a list of seemingly unrelated numbers, derived from a starting number (Called the "seed"). Changing the seed changes the random numbers you'll get. Some random number generators can be more complicated, but in many cases, having a repeatable output from a random number generator is useful, for example in Minecraft, the world seems random, but if you make a new world with the same seed as a previous world, everything will be generated exactly the same.

Comment
Add comment · Show 1 · 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 TommyZakh · Sep 28, 2015 at 06:36 AM 0
Share

I am working on a voxel project, and for testing I had set the seed as a constant number, but didn't realize until shortly after posting the question.

avatar image
0

Answer by Graham-Dunnett · Sep 27, 2015 at 05:16 PM

Come on @TommyZakh... Spending a few seconds doing some reading will help you out:

http://docs.unity3d.com/ScriptReference/Random-seed.html

:-)

Comment
Add comment · Show 1 · 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 TommyZakh · Sep 28, 2015 at 06:35 AM 1
Share

Don't worry, I already spent more than a few seconds reading.

:-)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Random.range multiple instantiations without repetition 1 Answer

Randomising list of integers and removing them from list once the task assigned to the integer in the project is completed 0 Answers

My Script Wont work? 2 Answers

reload random scene 2 Answers

Random number being generated 60 times when I only want one. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges