2

In my android app I need to generate random numbers,

My app will be running on many android devices in LAN and using random numbers to communicate with each other, I want to generate such a random and unique number that should never be same in any of the app in LAN.

It should be unique every time any user will generate it ,in his device and in the whole LAN.

Any ideas, I shall be grateful ???

Simon
  • 14,407
  • 8
  • 46
  • 61
Talib
  • 1,134
  • 5
  • 31
  • 58
  • 2
    You should read this http://stackoverflow.com/questions/2982748/create-a-guid-in-java and check out some of the links. To do what you need, you are going to have to use some algorithm which hashes, amongst other things, the NIC and time. The NIC is guaranteed to be unique. Sprinkle some time on it and follow up with some entropy, perhaps something else hardware based, and you should be good. – Simon Sep 08 '13 at 19:50
  • Would 32 bit integers be sufficeint? Also, due to the fact that you want to eliminate duplicates, which other statistical properties of randomness are you willing to drop? Why can't you just count sequentially (rather like Java does when assigning thread identifiers)? @Simon's link has legs though. – Bathsheba Sep 08 '13 at 19:51
  • 32 bit should be sufficient and I just need random and unique !! @Bathsheba – Talib Sep 08 '13 at 19:55
  • @Talib, does it need to be 'cyrptographically' random or just 'quite' random? – Bathsheba Sep 08 '13 at 19:56
  • @Bathsheba am not concerned about security or encryption, just random and always unique !! – Talib Sep 08 '13 at 19:59
  • If you really mean "always", you will need to something other than a random number generator. How often will you generate these numbers? – Simon Sep 08 '13 at 20:01
  • it just depends upon the user it may take few seconds or may be hours @Bathsheba – Talib Sep 08 '13 at 20:03
  • 2
    Then you should be looking at UIDs. – Simon Sep 08 '13 at 20:07

4 Answers4

2

Given the clarifications in the question comments, here's what I'd do: build a (deterministic) scheme that swaps bits in a 32 bit integer. Swap as many as you want. Call this the 'scrambler'.

Then just count through the integers; calling the 'scrambler' on each one.

Uniqueness is guaranteed, will be fast but not crytographically secure due to the deterministic nature of the swapping scheme. But note that in general this will have poor statistical randomness properties so don't ever use it when uniformly distributed random numbers are required (such as Monte Carlo). But I think it should satisfy your requirements.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

I am not familiar with Android. However, your problem seems to be addressed by UUID, or Universally Unique Identifier. A quick google yields this page.

In particular, you are looking for the method randomUUID(), which generates a variant 2, version 4 (randomly generated number) UUID as per RFC 4122.

Escualo
  • 40,844
  • 23
  • 87
  • 135
1

This is how you generate a random number:

Random r = new Random();
int random = r.nextInt(100); // returns a value between 0 (incl) and 100 (excl)

Random r = new Random();
int random = r.nextInt(100) + 20; // returns a value between 20 (incl) and 120 (excl)

Concerning your issue that the numbers should be unique, you could probably save each generated number somehow. Then, when drawing a new Random number check the saved numbers if the new random number has already been generated. If so, redraw.

Java Random doc: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
1

I actually find your requirements contradictory. A valid random sequence will not generate unique values otherwise it is not random. What I mean is you don't need anything random at all you just need the numbers to be unique.

To generate a unique number per network node is pretty easy combine machine ip or mac address with time. To get time simply do System.currentTimeMillis(); to get your ip address then use the example here how to get mac address in Java. Combining these two numbers will buy you uniqueness.

SkyWalker
  • 13,729
  • 18
  • 91
  • 187
  • you could simply erase the most significative bits of the date and fill them with the ip information. Doing so will intuitively erase how many exact millis from 1970 to say e.g. 1990. Perhaps you could even use a String or a UDT, who says it has to be a number? – SkyWalker Sep 08 '13 at 20:38