i have an array of 10 members. i need to fill it up with random numbers... say (1,30). however, there shouldn't be any duplicates. i can't seem to get the logic right. i'm coding in C#. help...
Here is what I did
In my business layer objects I have a class "Account"
that Implements IComparable
' a random number generator
Private Shared random As New random()
' the random number for this answer
Private rand As Integer = random.Next()
In the constructor...
'create a random number generator to be shared by all Accounts
random = New Random()
Public Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo
' ask the random numbers in each objects to compare themselves
Return rand.CompareTo(CType(obj, Answer).rand)
End Function
In the aspx page... (code behind)
Dim randomize As New ArrayList() 'the arrayList was filled by sql call
randomize.Sort()
I mostly work in vb but I think this is how it would look in c#
class Account : IComparable
{
// a random number generator to be shared by all Accounts
private static Random random;
// the random number for this account
private int rand = random.Next();
// in the constructor -
// create a random number generator to be shared by all accounts
random = new Random();
// compare this Account to the other one by their random values
public int CompareTo( object o )
{
// ask the random numbers in each objects to compare themselves
return rand.CompareTo( ((Account)o).rand );
}
// the rest of your account code goes here
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.