To generate a random number in C you can use the snippet below.
Sample C
#include <time.h> #include <stdlib.h> srand(time(NULL)); int r = rand();
To generate a random number in C you can use the snippet below.
#include <time.h> #include <stdlib.h> srand(time(NULL)); int r = rand();
To Shuffle a List in C# and VB.NET you can use the snippet below.
public static IList<T> ShuffleIList<T>(IList<T> inputList) { var cryptoServiceProvider = new RNGCryptoServiceProvider(); var count = inputList.Count; while (count > 1) { var bytes = new byte[1]; do cryptoServiceProvider.GetBytes(bytes); while (!(bytes[0] < count * (Byte.MaxValue / count))); var index = (bytes[0] % count); count--; var input = inputList[index]; inputList[index] = inputList[count]; inputList[count] = input; } return inputList; }
Public Shared Function ShuffleIList(Of T)(inputList As IList(Of T)) As IList(Of T) Dim cryptoServiceProvider = New RNGCryptoServiceProvider() Dim count = inputList.Count While count > 1 Dim bytes = New Byte(0) {} Do cryptoServiceProvider.GetBytes(bytes) Loop While Not (bytes(0) < count * ([Byte].MaxValue / count)) Dim index = (bytes(0) Mod count) count -= 1 Dim input = inputList(index) inputList(index) = inputList(count) inputList(count) = input End While Return inputList End Function
To get a random Color in C# and VB.NET you can use the following snippet.
public static System.Drawing.Color RandomColor() { var rand = new Random(); var r = rand.Next(0, 256); Thread.Sleep(1); var g = rand.Next(0, 256); Thread.Sleep(1); var b = rand.Next(0, 256); return System.Drawing.Color.FromArgb(255, r, g, b); }
Public Shared Function RandomColor() As System.Drawing.Color Dim rand As var = New Random Dim r As var = rand.Next(0, 256) Thread.Sleep(1) Dim g As var = rand.Next(0, 256) Thread.Sleep(1) Dim b As var = rand.Next(0, 256) Return System.Drawing.Color.FromArgb(255, r, g, b) End Function
To generate a random number in Python you can use the following snippet.
print random.randrange(0,10) #random number between 0 and 9
To make a random number in PHP you can use the following snippet.
$randInt = rand(9); // make a random number between 0 and 9
To select a random row in MySQL you can use the following query.
SELECT COLUMNNAME FROM TABLENAME ORDER BY RAND() LIMIT 1
To create a random number in javascript you can use the following snippet.
var random=Math.floor((Math.random() * 10) + 1);
Result
A random number between 1 and 10.
fore more informations see W3Schools JavaScript random() Method
To flip a coin in C# or VB.NET you just need to use this extension method on a instance of random.
public static bool FlipCoin(this Random rand) { return rand.Next(2) == 0; }
<System.Runtime.CompilerServices.Extension> _ Public Shared Function FlipCoin(rand As Random) As Boolean Return rand.Next(2) = 0 End Function
To generate random numbers in csharp or vb.net you simply need to use the Random class which is located in the namespace System.
c# example
var rand = new Random(); int randInt1 = rand.Next(1, 10); int randInt2 = rand.Next(10);
vb.net example
Dim rand = New Random() Dim randInt1 As Integer = rand.Next(1, 10) Dim randInt2 As Integer = rand.Next(10)
remember that the first rand.Next statements will return a number between 1 and 9 while the second rand.Next statement will return a number between 0 and 9.
Tip
If you do new Random() in a Loop the Random is initialised again and again, your Random function can then possibly return the same number several times.
You should keep a single Random instance and keep using Next on it without reinitialising.