-1

This code is obviously a random number generator, but how do i get it to be unique in the simplest way possible?

import java.util.Random;


public class Scramble {

public static void main(String[] args) {

            for (int i=0; i < 10; i++)
            {

            Random randomGenerator = new Random();
            int n = randomGenerator.nextInt(10);

            System.out.println("Random number is " +n);
            }

      }

}
user1360211
  • 1
  • 2
  • 3
  • 1
    You need to understand that 'unique' and 'random' are mutually contradictory. The more numbers in the sequence you collect, the easier it becomes to predict the next one, so the sequence is far from 'random'. – user207421 Apr 27 '12 at 10:26

5 Answers5

6

For this small number of possible values:

  1. Generate a list of all possible values
  2. Shuffle it
  3. Return the next item in each step
Joey
  • 344,408
  • 85
  • 689
  • 683
2

Does maximal-length LFSR (e.g., PRBS sequences) fits?

For instance, the PRBS31 sequence (x31+x28+1) is guaranteed to generate every 31-bit integer (except 0) only once in a cycle (which is 231-1 bits long)

It is fairly easy to implement, too:

public int prbs31(int state) {
    int feedback = ((state >> 30) ^ (state >> 27)) & 1;
    return ((state << 1) | feedback) & 0xffffffff;
}

You start with some (non-zero!) integer, and call prbs31 successively - passing the previous result back in. (it's a feedback register!)

PRBS31 generates very good statistically-random bit patterns (which is not to be confused with truly random bit patterns)

However, keep in mind that adjacent values will be very similar - the method suggested above does a one-bit sliding-window over the PRBS sequence, meaning every adjacent values have 30-bits in common (though in different places)

But we can advance the register 31 steps each time, like so:

// initial seed, doesn't really matter which value you use
// as long as it's not zero (remember that the sequence goes over
// all the possible 31-bits values anyway)
private static final int SEED = 0x55555555;

private int state = SEED;

public int nextInt() throws SequenceCycleException {
    for (int i = 0; i < 31; ++i) {
        state = prbs31(state);
        if (state == SEED) {
            throw new SequenceCycleException();
        }
    }
    return state;
}

This would generate a randomly-seeming sequence of integers that is (231-1)/31 long

DISCLAIMER: naive usage of a single LFSR is highly predictable. In this case, knowing one value gives knowledge of all future values - which makes this method good for simulations (such as games) but also very bad for anything with cryptographic or secret significance!

Personally, I use this method to generate unique IDs for objects that are used as keys in a hash table.

duvduv
  • 649
  • 6
  • 10
0

The simplest way possible is to use clock arithmetic. If you add by a number which is not a factor of 10 e.g. a prime (and the difference is not factor as well) you will get every possible value in a random walk. It not very random, but it is very simple.

e.g. say you pick 3 for 10 values.

3, 6, 9, 2, 5, 8, 1, 4, 7, 0
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Is this random in any useful way? – Louis Wasserman Apr 27 '12 at 05:49
  • When you only need a basic pseudo random such as walking bins of a hash map. (if you don't use a list) The beneifit is you don't need to retain any data structure or state other than the point you are looking at e.g. the hash. – Peter Lawrey Apr 27 '12 at 07:19
0

You could generate a random number, store it in an Array/HashMap/Whatever and return it. Then you just have to check each time if the new number is already in your saved old ones. Not very efficient but easy.

Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
Matthias
  • 5
  • 1
  • 2
-2

This is the code sample for random number(integer) generation. Being we know that Math.random() always returns double type value. So , I have converted it into int.

class RNumber{
    public static void main(String str[]){
        int countNum=100;   
        for(int i=0;i<countNum;i++){
            System.out.println("Random Unique values="+(int)(Math.random()*100));
        }
    }
}

Hope, it will help you.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
JDGuide
  • 6,239
  • 12
  • 46
  • 64