bluedragon99
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!
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>
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
The code you posted doesn't seem to be using the value returned. How do you intend to use it?
Jim
ASKER
<%@ 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>
ASKER
1066211
1563676
2775668
2775668
8568747
1563676
2775668
2775668
8568747
I will scratch my head for a time and see if I can think of an alternative.
Jim
Jim
I think I have a solution. Try the code below.
Jim
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>
ASKER
trying...
ASKER
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:
Sorry. This will fix it.
Jim
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>
ASKER
1999892
5073422
6839206
7176987
7600909
7600909
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>
Are you testing this on a web farm?
Jim
Jim
ASKER
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
Jim
ASKER
any random number would be perfectly fine..
Excellect! Then this will work.
Jim
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>
ASKER
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) {
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
mid(System.Guid.NewGuid(). ToString(" N").ToUppe r(),1,5);
how do I capture the first five characters? (using as a filename)
how do I capture the first five characters? (using as a filename)
(System.Guid.NewGuid().ToS tring("N") .ToUpper() ).Substrin g(0,5);
Jim
Jim
int randomNum = new Random(unchecked((int)Date
Page events should take more than a single tick.
Jim