|
hi , i need to generate an array of random numbers which are all different. How can i do so? I am using a while loop and .contains of an arraylist. Can someone tell me please?
Thanks
|
|
|
|
|
Use the random class
int num = random.Next();
Bob
Ashfield Consultants Ltd
|
|
|
|
|
create empty list of ints called used.
N = number of random integers you want.
i = 1
R: generate random number r
if r is in used
goto R
add r to used
i++
if r < N
goto R
loop ends here
I'll leave it to you to translate that to code. Make sure you don't use goto !
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
i'd create an arraylist with all the numbers in order and an empty arraylist.
then get a random number minimum 0 and less than the length of the arraylist. move that item from the first list to the second and repeat until the first arraylist is empty.
As a separate thought, what would happen if you implemented Icomparable and randomly returned -1 or +1 and then used it for a sort?
|
|
|
|
|
hell it even works
al.Sort(r);
class Randomizer : IComparer
{
#region IComparer Members
Random r = new Random();
public int Compare(object x, object y)
{
if (x == r) return 0;
return r.Next(2) - 1;
}
#endregion
}
}
I'll leave it to anyone who wants to try it to build an arraylist of stuff and work out the type of r
|
|
|
|
|
Russell Jones wrote: implemented Icomparable and randomly returned -1 or +1 and then used it for a sort?
Interesting idea. but the distribution of your list would not be random.
It would depend on the sorting algorithm used.
Simon
|
|
|
|
|
that's true.
I seem to remember about one of the early poker sites that screwed up with their random shuffling routines allowing players to effectively read other people's cards. It's alot harder to get properly random output than it first appears.
Russell
|
|
|
|
|
Russell Jones wrote: i'd create an arraylist with all the numbers in order and an empty arraylist.
then get a random number minimum 0 and less than the length of the arraylist. move that item from the first list to the second and repeat until the first arraylist is empty.
We had this discussion before, Mr. Jones[^]
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
ha brilliant,
I can't believe that with the number of posts you must read in a day you can remember a post from March, let alone take time to find it.
I notice that Christian's suggestion of a custom sort must have stuck in my brain.
Point taken but my answer still stands
|
|
|
|
|
This will pick ten unique numbers in the range 1 to 100, sorted from lowest to highest:
int range = 100, cnt = 10;
int[] num = new int[cnt];
Random rnd = new Random();
for (int current = 1; cnt > 0; current++) if (rnd.Next(range--) < cnt) num[num.Length - (cnt--)] = current;
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
If you use .NET 3.5 you can also use a HashSet :
do { set.Add(rand.Next(min, max)); } while (set.Count < 10);
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Is there a way to create a dll with classes that incorporate Windows Forms?
I was thinking of collecting some of the functionality for Windows Forms I re-use but Visual Studio won't let me use the System.Windows.Forms namespace in a DLL.
Why is that anyway?
I mean surely the namespace itself is inside a dll that visual studio uses?
Is there any way to create a DLL with the System.Windows.Forms namespace?
Thanks in advance
|
|
|
|
|
Just add a dll to the toolbox...
|
|
|
|
|
What do you mean?
The project itself is a dll.
And the problem is, VS won't let me use the System.Windows.Forms namespace.
|
|
|
|
|
In Solution Explorer|References folder, right click and choose Add Reference, then select System.Windows.Forms from the list... then you can use the namespace anywhere in your solution
Dave
|
|
|
|
|
I know, but that didn't work.
I found the error though, my VS is apparently messed up.
When I add references, it doesn't always work.
I found out because Microsoft Enterprise Library references were added in one project but adding the references in a different project didn't work and I had to manually look for the dlls and add them as file references.
I suppose it's the same problem in this case.
|
|
|
|
|
Hi All,
I want to read a DTD file and show it in a tree view.
Thanks in advance.
Regards,
Chanzeb Chaudhary
|
|
|
|
|
That's nice. Now where is your problem?
|
|
|
|
|
string file=@"c:\sample.dtd"
xmlDocument dtd=new xmlDocument();
dtd.Load(file);
Whether the above piece of code works or not???
I have tested it against XML documents(e.g. "sample.xml")
|
|
|
|
|
I have a problem in the following coding
protected void DataList1_ItemDataBound1(object sender, DataListItemEventArgs e)
{
DBCode dbo = new DBCode(); //class
DataTable dt = new DataTable();
dt = dbo.getDataTable("select IsSold, IsNew from product");
int a, b;
int c = int.Parse(dt.Rows.Count.ToString());
for (int i = 1; i <= c-1; i++)
{
a = int.Parse(dt.Rows[i]["Issold"].ToString());
b = int.Parse(dt.Rows[i]["IsNew"].ToString());
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (a == 1)
{
Label imgsold = e.Item.FindControl("lblsold") as Label; imgsold.Visible = true;
}
else
{
Label imgsold = e.Item.FindControl("lblsold") as Label; imgsold.Visible = false;
}
if (b == 1)
{
Label imgnew = e.Item.FindControl("lblnew") as Label; imgnew.Visible = true;
}
else
{
Label imgnew = e.Item.FindControl("lblnew") as Label; imgnew.Visible = false;
}
}
}
}
I want to enable and disable a label control with respect to the data coming from the database . this is actually a image gallery where i want make the image displayed with a tag new and sold . I have used datalist control and a two labels inside for displaying new and sold. but this code is not working so please help i am stuck in middle of the project
for further reference visit www.painternet.com/gallery.aspx
ram
|
|
|
|
|
Hey Ram,
clarify some things first.
what is ListItemType? is it a data list view?
also you can change int c = int.Parse(dt.Rows.Count.ToString()); to
int c = dt.Rows.Count cos the return type here is already int.
In the server, what datatypes are used for IsNew and IsSold?
What are the errors, if any?
He who goes for revenge must first dig two graves.
|
|
|
|
|
Hi
I want to use SQL View in my project. Is there any class in C# having functionality to produce SQL View.
Thanks
Atul
|
|
|
|
|
You use it just like a table, expect you can modify it.
|
|
|
|
|
Hello,
i'm developing at the moment a server-application, what can i do when my server has no performance anymore for this software? i could install it on a second server, thats right, but my problem is then to get variables, lists and other resources from the first server. how is something like that possible?
because big web-services for example google doesn't run on only one server too, so how can i make this that i can run one server-process per server but can access to the resources, lists and so on from the other servers?
|
|
|
|
|
1. Redesign your application/algorithm to run better.
2. Use remoting to communicate.
You will likely have to redesign most of the structure to support a distributed computing environment.
|
|
|
|