Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Logic

Status
Not open for further replies.

Kijori

Technical User
Jan 22, 2002
67
SG
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...
 
I just accomplished something similar.

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
}

Now you can say...
ArrayList.Sort()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top