Categories
PHP
Javascript
MySQL
C#
VB
VB.NET
ASP.NET
Regex
Packaging & compression
General Web Tech
Tech Speak


Google


This website looks best on firefox.
 
Resource Center : C# : <Using the Random class in .NET to generate unique random numbers>

Using the Random class in .NET to generate unique random numbers

Posted by: Floresense Team
Pages: 1  2  

Random is a class from the System.Object namespace, and is used to generate random numbers.

Random rnd = New Random();
int val = rnd.Next();

The above code generates a random number with no range.. meaning it could be any non-negative number, limited only by the maximum value an int32 can hold in .NET  You can also know this max limit from int.maxValue which is a constant.

byte[] test = new byte[100];
rnd.NextBytes(test);

The above fills the byte array test with all random or junk values. Could be useful in test applications which need to load a test set of values and do tests on a process.

int val = rnd.Next(1, 100)

The above returns a random number between 1 and 100 including 1 but not 100. For including 100, use 101 for the maxValue.

double val = rnd.NextDouble();

The above returns a random value between 0.0 and 1.0 with decimal places.

FAQ

1. Are the random values unique?

No. They are generated based on a generic algorithm which doesn't test whether the new number generated was already generated in the current run of the application. This is to obey the fact that the value generated should really be generated only randomly and the generation algorithm does nothing to override that fact.

Take care of this though. If you create a separate method in your code to generate and return the random numbers, like below,

private int getRnd(int lowRange, int highRange)
{
    rnd = new Random();
    return rnd.Next(lowRange, highRange+1);
}

The implementation above very often generates the same random numbers when called multiple times within the program. 2 out of 10 times it happens that calling above code returns the same random number. This is possibly a bug with .NET

A possible solution is to work with one instance of the Random class instead of creating a new instance everytime we want a random number.. above code can be modified as below.

private static Random rnd = null;
private static int getRnd(
  int lowRange,
  int highRange)
{
    if (rnd==null)
        rnd = new Random();

    return rnd.Next(lowRange, highRange+1);
}

The above code could be called true random, and only occassionally we will have the same random number repeated, in my observation possibly just by coincidence.

But, we DO often require unique random numbers, which we have to code for in random number generator methods like the above.

Some common techniques are:

1. Load an arraylist with previously generated random numbers.. test the new random value generated in the arraylist using .Contains method, then generate a new number if arraylist already has the number.

2. There could be a possible deadlock in above technique, if at all the code generates the same random number, and it could easily go into an infinite loop testing-and-regenerating random numbers. A second solution which many programmers have come up with is as below.

1. create an array and load it sequentially with values 1 to 30.
2. through a loop, shuffle the values in the array as below.
-- setup a loop with index = 1 to 30
-- get a random number between 1 and 30,
-- use it as the index1 of array, and use index value of the loop as index2 of the array
-- swap the values of the array at these two indexes.
-- do loop for 30 times or more, to end up with an array of random, unique values between 1 and 30.
 

Public Class Form1
  Private Sub Button1_Click
(ByVal sender As Object, _
 ByVal e As System.EventArgs) _
 Handles Button1.Click





    Dim source(30) As Integer
    For I As Integer = 1 To 30
      source(I) = I
    Next


    Dim rnd As New Random
    Dim index, tmp As Integer
    For i As Integer = 30 To 1
Step -1
      index = rnd.Next(1, i + 1)
      tmp = source(index)
      source(index) = source(i)
      source(i) = tmp
    Next
  End Sub
End Class

Pages: 1  2  

Advertisement

2005 - 2008 © Floresense.com