- Home /
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?
hey. Did you figured out what your issue was? Did you resolve it?
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!
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.)
Using the sample code I'm seeing nothing but 6 in the debug log.
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.