Advertisement
Napolean

roll

Apr 22nd, 2024
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. Certainly! Below is a simple C# class that implements a provably fair dice roll system. The `RollResult` method accepts a multiplier, an optional nonce, and a house edge with a default value of 1.0. It uses SHA256 for hashing the combined seeds to ensure fairness.
  2.  
  3. ```csharp
  4. using System;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7.  
  8. public class ProvablyFairDice
  9. {
  10.     public string PublicSeed { get; set; }
  11.     public string PrivateSeed { get; set; }
  12.     public int Nonce { get; set; }
  13.  
  14.     public ProvablyFairDice(string publicSeed, string privateSeed)
  15.     {
  16.         PublicSeed = publicSeed;
  17.         PrivateSeed = privateSeed;
  18.         Nonce = 0; // Default starting nonce
  19.     }
  20.  
  21.     public double RollResult(double multiplier, int? nonce = null, double houseEdge = 1.0)
  22.     {
  23.         // Use the provided nonce or the class's nonce property if not set
  24.         int usedNonce = nonce ?? Nonce;
  25.  
  26.         // Increment the nonce for the next roll
  27.         Nonce++;
  28.  
  29.         // Combine the seeds with the nonce to get the roll hash
  30.         string rollHash = CreateHash($"{PublicSeed}:{PrivateSeed}:{usedNonce}");
  31.  
  32.         // Convert the hash to a roll number
  33.         double rollNumber = ConvertHashToRollNumber(rollHash);
  34.  
  35.         // Calculate the result with the house edge and multiplier
  36.         double result = rollNumber * (100 - houseEdge) * multiplier;
  37.  
  38.         return result;
  39.     }
  40.  
  41.     private string CreateHash(string input)
  42.     {
  43.         using (SHA256 sha256Hash = SHA256.Create())
  44.         {
  45.             // ComputeHash - returns byte array
  46.             byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  47.  
  48.             // Convert byte array to a string
  49.             StringBuilder builder = new StringBuilder();
  50.             for (int i = 0; i < bytes.Length; i++)
  51.             {
  52.                 builder.Append(bytes[i].ToString("x2"));
  53.             }
  54.             return builder.ToString();
  55.         }
  56.     }
  57.  
  58.     private double ConvertHashToRollNumber(string hash)
  59.     {
  60.         // Take the first 8 characters of the hash and convert them to a decimal
  61.         string subHash = hash.Substring(0, 8);
  62.         long decimalValue = Convert.ToInt64(subHash, 16);
  63.  
  64.         // Convert the decimal to a number between 0 and 100
  65.         double rollNumber = (decimalValue / (double)UInt32.MaxValue) * 100;
  66.         return rollNumber;
  67.     }
  68. }
  69. ```
  70.  
  71. This class uses the SHA256 hashing algorithm to ensure the fairness of the dice roll. The `RollResult` method calculates the result based on the combined seeds and nonce, applies the house edge, and then multiplies it by the given multiplier. The `Nonce` property is incremented after each roll to ensure that each roll is unique.
  72.  
  73. Please note that this is a basic implementation and might need further adjustments based on your specific requirements, such as integrating with a database for seed management or adding additional security measures. Also, ensure that you're in compliance with any legal regulations regarding gambling and online games in your jurisdiction.
  74.  
  75. Source: Conversation with Bing, 4/22/2024
  76. (1) github.com. https://github.com/sownthunder/Back_2_Da_Future/tree/f6bcd431cc77ba7d931c679ffa3d59db55dd36e4/New%20Unity%20Project%2FAssets%2FEasy%20Secure%20-%20Coins%2C%20Gems%2FScripts%2FClasses%2FSystem%2FSecurityClass.cs.
  77. (2) github.com. https://github.com/neyhos-studio/LauncherBack/tree/552d4d45bd4f16ddd2c70fd0f300caf6e645e203/Helpers%2FShaHash.cs.
  78. (3) github.com. https://github.com/YajKMA/Zeiterfassung/tree/afb7955598530d2a9be557e6b33ff90d5c356e22/Security%2FHashing.cs.
  79. (4) github.com. https://github.com/UtrackiWiktor/HomeOffice/tree/02873f46fa6d5f4033626132a159fab83bb06929/HomeOffice%2Fclasses%2FPasswords%2FPassword.cs.
  80. (5) github.com. https://github.com/ShareNewsLLC/ShareNewsMVC/tree/56fbfa3c6641945f117a5181ca623c59da070176/MVC%2FControllers%2FAdminController.cs.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement