0

I am using this method to create random numbers Adding to element value in xml in another method. some files that are same won't compare. because numbers I am inserting are random not unique anymore. How to insert same number if file1name equals file1name. else random numbers.

Random random = new Random();
    ArrayList<Integer> arrayList = new ArrayList<Integer>();

    while (arrayList.size() < 6) {
        int a = random.nextInt(49)+1; 

        if (!arrayList.contains(a)) {
            arrayList.add(a);
        }
    }

Thanks in advance! new still a lot to learn.

dbc
  • 104,963
  • 20
  • 228
  • 340
Sam
  • 39
  • 1
  • 6
  • A unique number may be something like `System.currentTimeMillis()` unless it is a busy multi-threaded system. – Scary Wombat Apr 14 '20 at 04:20
  • good one. I also thought about that actually. but not feasible in my case. hundred of files compared at a time. – Sam Apr 14 '20 at 04:34
  • Maybe create a singleton who's sole function is to provide you with these numbers. Maybe utilize a `ArrayBlockingQueue` – Scary Wombat Apr 14 '20 at 04:40
  • Rather than an integer, a [`UUID`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/UUID.html) generated by `UUID.randomUUID()` might suit your needs. See [How good is Java's UUID.randomUUID?](https://stackoverflow.com/q/2513573/3744182) and maybe [Efficient method to generate UUID String in JAVA (UUID.randomUUID().toString() without the dashes)](https://stackoverflow.com/q/3804591). – dbc Aug 18 '20 at 03:01

0 Answers0