#Time.time.GetHashCode() always returning 0

1 messages · Page 1 of 1 (latest)

craggy token
#
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);
                }
            }
        }
    }
}
#

If I check useRandomSeed in unity Inspector, seed is set to 0 when the code runs.

fast meadow
#

okay, and what's the problem

craggy token
#

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.

fast meadow
#

Correct

vague elbow
#

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)

dawn moth
#

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

craggy token
#

Thanks for the detailed explanation!

acoustic violet
fast meadow
#

A hash is any kind of function that converts a value to a deterministic number, I'd argue it doesn't have to be chaotic, just it needs to reduce chance of collisions

#

for integers or floats why do some manipulation... you've already got a collision free hash right there in the bytes