9 Replies Latest reply: Dec 7, 2007 1:30 AM by 807603 RSS

    Random Number without duplication.

    807603
      Hi,
      i wanted to genreate a random number (long),but the number should be unique, i want some code....method that will genreate random number within milisecond....without any duplication.

      Random class has problem that ......nextLong() method takes lots of time to execute....can any one suggest me any other suggestion...?
        • 1. Re: Random Number without duplication.
          807603
          You need to get better requirements of what you actually need because the terms "random number" and "no duplicates" are not compatible.

          So come up with some coherent, clear and possible requirements of what you actually need.
          • 2. Re: Random Number without duplication.
            807603
            All i wanated to say.......i wanted to know any method in java which can genreate random number (long) .

            same thing can be achived using java.util.Random class but .......if u hit nextint() of random class within 1ms it may possible u may get same number.

            Also Random.nextLongt() takes some time to execute and produce

            So i just want another method which can genreate random number (preferably long) without loss of time.

            I hope it is clear now.
            • 3. Re: Random Number without duplication.
              807603
              Please post your formatted code.
              • 4. Re: Random Number without duplication.
                807603
                i dont have code. but i wanted to know about it.
                i think still it is not clear.

                i will put in simple words.......i wanted to know any method/class in java
                (we are using jdk1.4) which can genreate random numbers within ms/microsec/nanosec.....

                for eg .....System.currentTimeMillis()) will produce random numbers ......but if u print it 2,3 times it produces same result.

                System.out.println(System.currentTimeMillis());
                     System.out.println(System.currentTimeMillis());     
                     System.out.println(System.currentTimeMillis());

                output:
                1197011402447
                1197011402447
                1197011402447

                i want new number every time.....!!!!!!
                • 5. Re: Random Number without duplication.
                  807603
                  prady_jo wrote:
                  i dont have code. but i wanted to know about it.
                  sigh

                  Why are you making it so difficult to help you?
                  i think still it is not clear.
                  No it's very clear. You have great misunderstandings all over the place.

                  What I suspect at this point is actually happening is that you were using nextInt but generating a new Random object each time. And getting duplicate results because it was using the same seed.

                  A problem that would be easily resolved by simply re-using the same Random object.

                  But instead you decided that the problem was that you needed to use longs instead of ints and are now having other problems.

                  Where the code using currentTimeMillis() fits into any of this is not really clear but it's also not important.

                  So why don't you please post code that demonstrates exactly what problem you are having. Then this code can be corrected. The problem is not with the Random class the problem is with your code, that you are so far refusing to show. If you want help post your code.
                  • 6. Re: Random Number without duplication.
                    807603
                    prady_jo wrote:
                    for eg .....System.currentTimeMillis()) will produce random numbers
                    No, it won't. The numbers from currentTimeMillis are not random at all. They're the number of milliseconds that have passed since a particular point in time. Not only are they not random, they're strictly increasing and highly predictable.



                    ......but if u print it 2,3 times it produces same result.

                    System.out.println(System.currentTimeMillis());
                         System.out.println(System.currentTimeMillis());     
                         System.out.println(System.currentTimeMillis());

                    output:
                    1197011402447
                    1197011402447
                    1197011402447

                    i want new number every time.....!!!!!!
                    Wait a millisecond (or longer if you're using an OS that can't handle millisecond granularity) and you'll get a new number. That's the nature of time. Similarly, if today is Thursday, and you wait a day, you'll find that it's Friday.


                    You either have an extremely poor grasp of what "random" means, or such a poor grasp of time that you must have suffered a head injury, or you have very poor observational skills when you failed to notice the word "time" in "currentTimeMillis".
                    • 7. Re: Random Number without duplication.
                      807603
                      if u know any method that genreates random number apart from Random class methods ....pleas let me know.
                      • 8. Re: Random Number without duplication.
                        807603
                        prady_jo wrote:
                        if u know any method that genreates random number apart from Random class methods ....pleas let me know.
                        Yes.

                        Find a hammer. Smack yourself in the head with it. When you wake up the first number you think of write down on a piece of paper. Then repeat. When you have enough numbers or you forget why you are doing this in the first place, whichever comes first, stop.

                        Alternatively post your code. You originally claimed that you had code that doesn't work for you, then you claimed you didn't have code. If you don't have code then write some. If you do have code then post it.
                        • 9. Re: Random Number without duplication.
                          807603
                          prady_jo wrote:
                          if u know any method that genreates random number apart from Random class methods ....pleas let me know.
                          Perhaps you can give a good reason why Random isn't good enough. "It produces duplicates" isn't a good reason, because that's what "random" entails. Actually describing in detail what you're trying to accomplish would be nice as well.


                          But you know, I'm going to use my amazing psychic powers now....OK...I'm remote viewing your skull....there's an echo here...

                          OK. My remote viewing has told me that what you're really trying to accomplish is to generate globally unique identifiers, and you're trying to roll your own solution by misusing and misunderstanding currentTimeMillis() and the meaning of randomness. Try Googling "globally unique identifiers".