c# randomize duplicates using now.datetime.milliseconds seed

I have the following code I am using as an asp upload form.   This line :

int randomNum = new Random().Next(1000000, 9000000);

is generating duplicate numbers if run very quickly due to the fact that it uses the system time as a seed.  How can I fix this?  Please provide code I can test and points will be awarded quickly!  Thanks!


<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
 
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
	
int randomNum = new Random().Next(1000000, 9000000);
    
   foreach(string f in Request.Files.AllKeys) {
       HttpPostedFile file = Request.Files[f];
       file.SaveAs("Z:\\ThreatUploads\\ScanMe\\" + file.FileName);
   }
}
</Script>

Open in new window

LVL 1
bluedragon99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimBrandleyCommented:
Try using system ticks for the seed as:

int randomNum = new Random(unchecked((int)DateTime.Now.Ticks)).Next(1000000, 9000000);

Page events should take more than a single tick.

Jim
0
bluedragon99Author Commented:
Just tried it, still getting occasional duplicates...not sure exactly why as you would think there would be enough of a delay
0
JimBrandleyCommented:
One tick is 100ns. I do not understand how you are firing successive page load events in less time than that unless you have a very fast server and lots of users.

The code you posted doesn't seem to be using the value returned. How do you intend to use it?

Jim
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

bluedragon99Author Commented:

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
 
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
	
int randomNum = new Random(unchecked((int)DateTime.Now.Ticks)).Next(1000000, 9000000);
 
    
   foreach(string f in Request.Files.AllKeys) {
       HttpPostedFile file = Request.Files[f];
        file.SaveAs("Z:\\ThreatUploads\\ScanMe\\ScanThis_55185588\\" + randomNum + "_" + file.FileName);
   }
}
</Script>

Open in new window

0
bluedragon99Author Commented:
1066211
1563676
2775668
2775668
8568747
0
JimBrandleyCommented:
I will scratch my head for a time and see if I can think of an alternative.

Jim
0
JimBrandleyCommented:
I think I have a solution. Try the code below.

Jim

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
 
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
   
   int seed = 0;
   Object seedObject = Application["RndFileSeed"];
   if (seedObject == null)
      seed = unchecked((int)DateTime.Now.Ticks);
   else
      seed = (int)seedObject;
 
   int randomNum = new Random(seed).Next(1000000, 9000000);
 
    
   foreach(string f in Request.Files.AllKeys) {
       HttpPostedFile file = Request.Files[f];
        file.SaveAs("Z:\\ThreatUploads\\ScanMe\\ScanThis_55185588\\" + randomNum + "_" + file.FileName);
   }
   Application.Lock();
   Application["RndFileSeed"] = randomNum.Next();
   Application.UnLock();
 
}
</Script>

Open in new window

0
bluedragon99Author Commented:
trying...
0
bluedragon99Author Commented:

Server Error in '/' Application.
--------------------------------------------------------------------------------
 
Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
 
Compiler Error Message: CS0117: 'int' does not contain a definition for 'Next'
 
Source Error:
 
 
 
Line 22:    }
Line 23:    Application.Lock();
Line 24:    Application["RndFileSeed"] = randomNum.Next();
Line 25:    Application.UnLock();
Line 26:  
 

Open in new window

0
JimBrandleyCommented:
Sorry. This will fix it.

Jim

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
 
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
   
   int seed = 0;
   Object seedObject = Application["RndFileSeed"];
   if (seedObject == null)
      seed = unchecked((int)DateTime.Now.Ticks);
   else
      seed = (int)seedObject;
 
   Random rnd = new Random(seed);
   int randomNum = rnd.Next(1000000, 9000000);
 
    
   foreach(string f in Request.Files.AllKeys) {
       HttpPostedFile file = Request.Files[f];
        file.SaveAs("Z:\\ThreatUploads\\ScanMe\\ScanThis_55185588\\" + randomNum + "_" + file.FileName);
   }
   Application.Lock();
   Application["RndFileSeed"] = rnd.Next();
   Application.UnLock();
 
}
</Script>

Open in new window

0
bluedragon99Author Commented:
1999892
5073422
6839206
7176987
7600909
7600909
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
 
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
   
   int seed = 0;
   Object seedObject = Application["RndFileSeed"];
   if (seedObject == null)
      seed = unchecked((int)DateTime.Now.Ticks);
   else
      seed = (int)seedObject;
 
   Random rnd = new Random(seed);
   int randomNum = rnd.Next(1000000, 9000000);
    
   foreach(string f in Request.Files.AllKeys) {
       HttpPostedFile file = Request.Files[f];
        file.SaveAs("Z:\\ThreatUploads\\ScanMe\\ScanThis_433609376\\" + randomNum + "_" + file.FileName);
   
}
   Application.Lock();
   Application["RndFileSeed"] = rnd.Next();
   Application.UnLock();
 
}
</Script> 

Open in new window

0
JimBrandleyCommented:
Are you testing this on a web farm?

Jim
0
bluedragon99Author Commented:
1 IIS 6.0 server
0
JimBrandleyCommented:
Is the 7 digit randomizer a requirement, or could you take any random value? I'm wobdering if a GUID would better serve your needs.

Jim
0
bluedragon99Author Commented:
any random number would be perfectly fine..
0
JimBrandleyCommented:
Excellect! Then this will work.

Jim

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
 
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
   string guid = System.Guid.NewGuid().ToString("N")).ToUpper();
 
   foreach(string f in Request.Files.AllKeys) {
       HttpPostedFile file = Request.Files[f];
        file.SaveAs("Z:\\ThreatUploads\\ScanMe\\ScanThis_55185588\\" + guid + "_" + file.FileName);
   }
}
</Script>

Open in new window

0
bluedragon99Author Commented:
misising semi colon somewhere?
Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
 
Compiler Error Message: CS1002: ; expected
 
Source Error:
 
 
 
Line 6:  <Script language="C#" runat=server>
Line 7:  void Page_Load(object sender, EventArgs e) {
Line 8:     string guid = System.Guid.NewGuid().ToString("N")).ToUpper();
Line 9:   
Line 10:    foreach(string f in Request.Files.AllKeys) {
 

Open in new window

0
JimBrandleyCommented:
Nope - Unbalanced parens. It's fixed below.

Jim

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
 
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
   string guid = System.Guid.NewGuid().ToString("N").ToUpper();
 
   foreach(string f in Request.Files.AllKeys) {
       HttpPostedFile file = Request.Files[f];
        file.SaveAs("Z:\\ThreatUploads\\ScanMe\\ScanThis_55185588\\" + guid + "_" + file.FileName);
   }
}
</Script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bluedragon99Author Commented:
mid(System.Guid.NewGuid().ToString("N").ToUpper(),1,5);


how do I capture the first five characters?  (using as a filename)
0
JimBrandleyCommented:
(System.Guid.NewGuid().ToString("N").ToUpper()).Substring(0,5);

Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.