using UnityEngine;
public class MpaGenerator : MonoBehaviour
{
public int width;
public int height;
public int exitSize;
public string seed;
public bool useRandomSeed;
[Range(0, 100)]
public int fillPercent;
int[,] map;
void Start()
{
GenerateMap();
}
void GenerateMap()
{
map = new int[width, height];
RandomFillMap();
}
void RandomFillMap()
{
if (useRandomSeed)
{
seed = Time.time.ToString();
}
System.Random random = new System.Random(seed.GetHashCode());
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
map[x, y] = random.Next(0, 100) < fillPercent ? 1 : 0;
}
}
}
private void OnDrawGizmos()
{
if (map != null)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Gizmos.color = (map[x,y] == 0) ? Color.white : Color.black;
Vector3 pos = new Vector3(-width / 2 + x + .5f, -height / 2 + y + .5f, 0);
Gizmos.DrawCube(pos, Vector3.one);
}
}
}
}
}
#Time.time.GetHashCode() always returning 0
1 messages · Page 1 of 1 (latest)
okay, and what's the problem
Shouldn't the hascode of time.time be different every time I run it?
I am following a tutorial with this same code, but the guy when checks useRandomSeed, gets different result each time.
Okay, my bad, I got it! I thought Time.time returns current time or something, but it returns time application is running for. So It will always be 0 when I press play.
Correct
Rather than using time.time, I'd recommend just using Random.Next() for the time being until you get onto a more advanced seeding system (if that's what you need)
It's also worth noting that GetHashCode is not a random thing, it's computed based on the value
For integer types, the value itself is returned. i.e. 5.GetHashCode() just returns 5.
For other numeric types, the value is reinterpreted as an integer and that same value is returned
For example, 5.0f.GetHashCode() returns 1073741824.
If you were to view how 5.0f and 1073741824 are both stored in memory, it'd be the same (i.e. they both correspond to the bytes 0x00 0x00 0x00 0x40)
Which means 5.0f.GetHashCode() and 1073741824.GetHashCode() both return the same value, 1073741824
And it just so happens that the value 0f as a float is the same bytes in memory as 0 as an int, which is why you're getting 0 as the hash code
Thanks for the detailed explanation!
Sorry to ask in a solved question but this sounded interesting, getting a hash code isn't like supposed to hash your value to make it deterministicly random? (Seemingly random but actually derived from chaotic number manipilation)