Hi ayla bibi
Thanks for using the Q&A platform.
Microsoft recommends using the System.Security.Cryptography.RandomNumberGenerator class for any security sensitive random number generation in .NET. It provides cryptographically strong randomness, unlike the insecure Random class. Specifically, RandomNumberGenerator.GetInt32(0, 2) offers a secure way to generate a random 0 or 1 ideal for coin flips and is explicitly documented to use a strong RNG under the hood.
Find documentation:
https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.randomnumbergenerator?view=net-9.0
https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.randomnumbergenerator.getint32?view=net-9.0
Regarding returning the result to the frontend, you can use an Azure function to return a small JSon payload, and then your frontend uses the fetch(......).json() pattern to parse and display the result.You can find additional documentation:
https://learn.microsoft.com/en-us/azure/static-web-apps/add-api?tabs=vanilla-javascript
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cfunctionsv2&pivots=programming-language-csharp
For best practices:1. Use cryptographic RNG RandomNumberGenerator, which is secure and not predictable from previous outputs.
- Avoid exposing seeds or intermediate values that only return "Heads" or "Tails".
- Rate limiting requests prevents brute forcing by spamming the endpoint.
- Logging results server-side is useful for audit/troubleshooting.
- Use HTTPS to prevent tampering during transit.
- Protect the Function
If the response was helpful, please feel free to mark it as “Accepted Answer” and consider giving it an upvote. This also benefits others in the community.
Regards,
Obinna.