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?

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)

  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!