Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Not the best example in the book

Not the best example in the book

Scheduled Pinned Locked Moved The Weird and The Wonderful
questionlearningsysadmintutorialannouncement
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BotCar
    wrote on last edited by
    #1

    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.

    P M 2 Replies Last reply
    0
    • B BotCar

      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.

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      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

      B Kornfeld Eliyahu PeterK 2 Replies Last reply
      0
      • P Peter_in_2780

        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

        B Offline
        B Offline
        BotCar
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • P Peter_in_2780

          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

          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu Peter
          wrote on last edited by
          #4

          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)

          "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

          B 1 Reply Last reply
          0
          • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

            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)

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • B BotCar

              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.

              M Offline
              M Offline
              Matthew Dennis
              wrote on last edited by
              #6

              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();

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups