Ark223 / BGA

Bridge Gameplay Analysis
MIT License
0 stars 2 forks source link

Seed the random function to get reproduceable result #4

Closed ThorvaldAagaard closed 7 months ago

ThorvaldAagaard commented 7 months ago

I recommend to seed the random function like this

            if (seed == 0)
            {
                seed = CalculateSeed(this.northHand.ToString() + this.southHand.ToString());
                this.random = new Random(seed);
            }

            this.LoadCombinations();
        }

        static int CalculateSeed(string input)
        {
            // Calculate the SHA-256 hash
            using (SHA256 sha256 = SHA256.Create())
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);
                byte[] hashBytes = sha256.ComputeHash(inputBytes);

                // Convert the first 4 bytes of the hash to an integer and take modulus
                int hashInteger = BitConverter.ToInt32(hashBytes, 0);
                return hashInteger;
            }
        }

in SetupEvaluation

I am using the 2 hands concatenated as input, but perhaps you want to add it as an input field.

Ark223 commented 7 months ago

It will be added in the next update. Thanks!