Link to home
Start Free TrialLog in
Avatar of bluedragon99
bluedragon99Flag for United States of America

asked on

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

Avatar of JimBrandley
JimBrandley
Flag of United States of America image

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
Avatar of bluedragon99

ASKER

Just tried it, still getting occasional duplicates...not sure exactly why as you would think there would be enough of a delay
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

<%@ 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

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

Jim
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

trying...

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

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

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

Are you testing this on a web farm?

Jim
1 IIS 6.0 server
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
any random number would be perfectly fine..
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

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

ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mid(System.Guid.NewGuid().ToString("N").ToUpper(),1,5);


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

Jim