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 alemimi · Jan 29, 2014 at 06:05 PM · randomrangeseed

Random.Range affects the Random.seed

In the last weeks I made a game with a Procedural Map (full of Random.Ranges), now I want to "be able" to set or control the seed; The problem is that apparently when I use Random.Range, the seed changes while using it, and then turns back to the seed I wanted to have; This is making the game generate really weird Maps (like all straight corridors and 1 turn right), it's practically breaking my game. If I don't try setting the seed, I get normal random Maps (lots of rooms, corridors of any type etc..).

The Code is too long for you to read it, it would be useless, but I'm going to take a few important lines:

 using UnityEngine;
 using System.Collections;
 
 public class StartGame : MonoBehaviour {
 
     int randomSeedS;
 
     // Use this for initialization
     void Start () {
         randomSeedS = (int)System.DateTime.Now.Ticks;
     }
 
     void Update () {
         Random.seed = randomSeedS;
     }

Then in another Script I wrote a code to show it in the game as a GUI.Label. Is there any way for the seed to not be affected by Random.Range?

Comment
Add comment · Show 1
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 Fragmental · Nov 12, 2015 at 08:10 PM 0
Share

hey. Did you figured out what your issue was? Did you resolve it?

1 Reply

· Add your reply
  • Sort: 
avatar image
6

Answer by superdupergc · Feb 21, 2014 at 02:51 AM

The Random.Range function does not affect the seed, but it does affect which numbers you get the next time you call Random.Range. It's only deterministic if you set the seed, then call random.range the same number of times.

Try this:

  • Set Random.seed once to a known value, like 12345.

  • Call your procedural generation algorithm several times

Each time, you see the same level generated as long as you're doing the procedural generation steps in the same order, and you're not setting the seed more than once.

Setting the seed every update isn't something you should ever need to do. This is the sort of behavior you could expect (the numbers are made up)

 Update(){
   Random.seed = 12345;
   Debug.Log(Random.Range(0,10)); //returns 6 every time
   Debug.Log(Random.Range(0,10)); //returns 2 every time
 }

As long as you're not setting the Random.seed anywhere else, and this update code isn't interrupted by another call to Random.anything, you should see the same values every time.

I hope this helps!

Comment
Add comment · Show 3 · 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 Ardivaba · Mar 01, 2015 at 05:57 PM 1
Share

Well i just wanted to stop you @: "You should ever need to do". Using DateTime.Now as Random.seed is absolutely amazing to use if you want to "sync" randomness in a multiplayer game without sending any information. "Eg, Randomly moving NPC's (fireflies?) that are synced across all clients without sending any packets to do that at all" (Yes, it'll be headache to actually make sure that calls to Random.* are "lined" up properly but it's definitely doable.)

avatar image Fragmental · Nov 12, 2015 at 09:43 PM 0
Share

Using the sample code I'm seeing nothing but 6 in the debug log.

avatar image ijidau Fragmental · Nov 12, 2015 at 11:08 PM 0
Share

A false positive. Add a third random range and you'll get 6, 6, 9. It's a pure coincidence that seed 12345 will return two 6's in succession.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector 3 to float eg V3(1, 2, 3) -> float 123 in JS? 1 Answer

Random object, Array index out of range 0 Answers

several Random instances 1 Answer

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

How do re-randomize Random.seed after setting it? 3 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