User Tools

Site Tools


notes:csharp:random

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

notes:csharp:random [2015/06/24]
admin created
notes:csharp:random [2016/12/13] (current)
admin
Line 1: Line 1:
 ====== Random Numbers in C# ====== ====== Random Numbers in C# ======
  
-Generating ​random numbers ​scaling and shifting: n = a + rand() % b+Generate ​random numbers ​by scaling and shifting: n = a + rand() % b
   * a is the //​shifting//​ value (the first number in the desired range)   * a is the //​shifting//​ value (the first number in the desired range)
-  * b is the //scaling// factor (the width of the desired range) b = upper_range ​lower_range ​+ 1+  * b is the //scaling// factor (the width of the desired range) b = UpperRange ​LowerRange ​+ 1
  
 Any public static members of the //Random// type are thread safe. Any instance members are not guaranteed to be thread safe.  Any public static members of the //Random// type are thread safe. Any instance members are not guaranteed to be thread safe. 
  
-**Example**: Use the //​System.Random//​ class:+Example: Use the //​System.Random//​ class:
 <code csharp> <code csharp>
-// Declare an object representing the random number generator.+// Create a random number generator.
 Random rnd = new Random(); Random rnd = new Random();
  
Line 23: Line 23:
  
  
-**Example**: Generate three random numbers (bytes) and insert them to a byte[] array:+Example: Generate three random numbers (bytes) and insert them into a byte[] array:
 <code csharp> <code csharp>
 Random rnd = new Random(); Random rnd = new Random();
Line 33: Line 33:
  
  
-**Example**: A handy helper class generating random numbers:+Example: A helper class for generating random numbers:
 <code csharp> <code csharp>
 class RandomHelper class RandomHelper
Line 39: Line 39:
     private Random rnd = new Random();     private Random rnd = new Random();
  
-    #region Singleton ​implementation+    #region Singleton
     private static RandomHelper instance;     private static RandomHelper instance;
     public static RandomHelper Instance     public static RandomHelper Instance
notes/csharp/random.txt ยท Last modified: 2016/12/13 by admin