
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to generate secure random numbers
For secure random numbers, use the RNGCryptoServiceProvider Class. It implements a cryptographic Random Number Generator.
Using the same class, we have found some random values using the following −
using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider()) { byte[] val = new byte[6]; crypto.GetBytes(val); randomvalue = BitConverter.ToInt32(val, 1); }
To generate random secure numbers, you can try to run the following code.
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Security.Cryptography; public class Demo { public static void Main(string[] args) { for (int i = 0; i <= 5; i++) { Console.WriteLine(randomFunc()); } } private static double randomFunc() { string n = ""; int randomvalue; double n2; using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider()) { byte[] val = new byte[6]; crypto.GetBytes(val); randomvalue = BitConverter.ToInt32(val, 1); } n += randomvalue.ToString().Substring(1, 1)[0]; n += randomvalue.ToString().Substring(2, 1)[0]; n += randomvalue.ToString().Substring(3, 1)[0]; n += randomvalue.ToString().Substring(4, 1)[0]; n += randomvalue.ToString().Substring(5, 1)[0]; double.TryParse(n, out n2); n2 = n2 / 100000; return n2; } }
Output
0.13559 0.0465 0.18058 0.26494 0.52231 0.78927
- Related Articles
- Python module to Generate secure random numbers
- Generate Secure Random Numbers for Managing Secrets using Python
- Java program to generate random numbers
- Java Program to generate random numbers string
- C++ Program to Generate Random Numbers Using Middle Square Method
- C++ Program to Generate Random Numbers Using Probability Distribution Function
- Java Program to generate n distinct random numbers
- Random vs Secure Random numbers in Java
- C++ Program to Generate Random Numbers Using Multiply with Carry Method
- C# Program to Generate Random Even Numbers Using LINQ Parallel Query
- Generate random numbers using C++11 random library
- C++ program to generate random number
- C++ program to generate random alphabets
- How to Automatically Generate Random Secure Passwords in Google Chrome?
- Java Program to generate random numbers with no duplicates

Advertisements