Not the best example in the book
-
I was in a training course last week where the below code was used as part of an example. It was described as a method that would generate a unique ID and add the specified prefix to it.
public static String generateId(String prefix)
{
int randomNumber = new Random().nextInt();String id = Integer.toString(randomNumber); if(id.startsWith("-")) { id = id.replace('-', '3'); id = prefix + id; } return id;
}
Apart from the flaws that I'm sure you'll spot, I should point out that this was in the context of server-side code that forms part of a workflow. So depending on how often the workflow executes, it could be likely that multiple instances of the workflow would execute at the same time and generate the same "unique" ID.
What is this talk of release? I do not release software. My software escapes leaving a bloody trail of designers and quality assurance people in its wake.
-
I was in a training course last week where the below code was used as part of an example. It was described as a method that would generate a unique ID and add the specified prefix to it.
public static String generateId(String prefix)
{
int randomNumber = new Random().nextInt();String id = Integer.toString(randomNumber); if(id.startsWith("-")) { id = id.replace('-', '3'); id = prefix + id; } return id;
}
Apart from the flaws that I'm sure you'll spot, I should point out that this was in the context of server-side code that forms part of a workflow. So depending on how often the workflow executes, it could be likely that multiple instances of the workflow would execute at the same time and generate the same "unique" ID.
What is this talk of release? I do not release software. My software escapes leaving a bloody trail of designers and quality assurance people in its wake.
BotCar wrote:
... multiple instances of the workflow would execute at the same time and generate the same "unique" ID.
It's worse than that. Many implementations of Random() use a fixed seed by default, so the first value returned is effectively constant. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
BotCar wrote:
... multiple instances of the workflow would execute at the same time and generate the same "unique" ID.
It's worse than that. Many implementations of Random() use a fixed seed by default, so the first value returned is effectively constant. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
That's interesting. I did not realize that. Do you mean that some languages' implementations of pseudorandom number generators uses fixed seeds, or that some implementations of the Java Virtual Machine uses fixed seeds by default?
What is this talk of release? I do not release software. My software escapes leaving a bloody trail of designers and quality assurance people in its wake.
-
BotCar wrote:
... multiple instances of the workflow would execute at the same time and generate the same "unique" ID.
It's worse than that. Many implementations of Random() use a fixed seed by default, so the first value returned is effectively constant. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
Fortunately .NET does it with changing seed...
public Random() : this(Environment.TickCount)
{
}I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
Fortunately .NET does it with changing seed...
public Random() : this(Environment.TickCount)
{
}I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
Yep. I'm not sure how fortunate that is, though, because it means you don't trip over this problem until your server is running at high load and you instantiate two requests at the same time. That's really really hard to replicate in a test environment.
-
I was in a training course last week where the below code was used as part of an example. It was described as a method that would generate a unique ID and add the specified prefix to it.
public static String generateId(String prefix)
{
int randomNumber = new Random().nextInt();String id = Integer.toString(randomNumber); if(id.startsWith("-")) { id = id.replace('-', '3'); id = prefix + id; } return id;
}
Apart from the flaws that I'm sure you'll spot, I should point out that this was in the context of server-side code that forms part of a workflow. So depending on how often the workflow executes, it could be likely that multiple instances of the workflow would execute at the same time and generate the same "unique" ID.
What is this talk of release? I do not release software. My software escapes leaving a bloody trail of designers and quality assurance people in its wake.
This does not generate UNIQUE ids, only RANDOM ids. There is nothing preventing the generation of an id that has been previously generated. If you want unique ids, use Guids.
String id = Guid.NewGuid().ToString();