How to generate a random number in ascending order

I would like to generate random numbers in ascending order like:, 0, 2, 3, 5 .. 100

but not2, 0, 5 ..

This is what I came up with:

public static int vol=5;    
public static void main(String[] args) {
    int randno = getRandNum();
    vol = vol+randno;   
    System.out.println(getRandNum());
}   
private static  int getRandNum() {
    Random r = new Random();
    for (int i =0; i<10; i++)
    {
        int v=r.nextInt(vol);
        System.out.println("r"+v);
    }
    return vol;
}

      

How could I achieve the above goal?

+2


source to share


3 answers


/**
 * Generates random numbers, returning an array of ascending order.
 * @param amount    The amount of numbers to generate.
 * @param max       The maximum value to generate.
 * @return  An array of random integers of the specified length.
 */
public static int[] generateIncreasingRandoms(int amount, int max) {
    int[] randomNumbers = new int[amount];
    Random random = new Random();
    for (int i = 0; i < randomNumbers.length; i++) {
        randomNumbers[i] = random.nextInt(max);
    }
    Arrays.sort(randomNumbers);
    return randomNumbers;
}

      

You can use it like this:

// Generates 10 random numbers of value 0 to 100,
// printing them in ascending order
for (int number : generateIncreasingRandoms(10, 100)) {
    System.out.print(number + " ");
}

      


Or if you are a micro-optimized person and don't want to sort,



/**
 * Generates random numbers, returning an array of ascending order.
 * @param amount    The amount of numbers to generate.
 * @param max       The maximum value to generate.
 * @return  An array of random integers of the specified length.
 */
public static int[] generateIncreasingRandomWithoutSorting(int amount, int max) {
    int[] randomNumbers = new int[amount];
    double delta = max / (float)amount;
    Random random = new Random();
    for (int i = 0; i < randomNumbers.length; i++) {
        randomNumbers[i] = (int)Math.round(i*delta + random.nextDouble() * delta);
    }
    return randomNumbers;
}

      

Use case:

// Generates 10 random numbers of value 0 to 100,
// printing them in ascending order
for (int number : generateIncreasingRandomWithoutSorting(10, 100)) {
    System.out.print(number + " ");
}

      

The reason each number is between 0-10, 10-20, 20-30 .. in this case is used if I just allow the whole range and you get 100 on the first try, you are going to get the whole array out of 100.

Being more controllable, with this solution, you don't really get what you are asking for ("10 numbers from 0 to 100 sorted in ascending order") as it changes the range for every consecutive number. (just like any other solution that doesn't require sorting)

+3


source


Ben Barcain's answer is good, but if you don't want to create a set of numbers in one step, but you want to get one number after another, you can do something like this:



private static final int MAX = 5;

private Random rand = new Random();
private int maxRand = 0;

public int getIncreasingRandomNumber() {
    maxRand = rand.nextInt(MAX);
    return maxRand;
}

      

+2


source


how about this?

public class increasing {
    public static void main (String[] args) { 
        Random r = new Random(); 

        int totalNums = 100; 
        int count = 0;

        int lastVal = 0;
        int currVal = 0;
        while(count < totalNums) { 
            currVal = r.nextInt(200);
            lastVal = lastVal + currVal; 
            System.out.println(lastVal + ",");
            count++;
        }
    }

}

      

+1


source







All Articles