Example usage for java.util Random nextInt

List of usage examples for java.util Random nextInt

Introduction

In this page you can find the example usage for java.util Random nextInt.

Prototype

public int nextInt(int bound) 

Source Link

Document

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Usage

From source file:Main.java

public static void main(String[] args) {
    int[] nums = new int[100];

    for (int i = 0; i < nums.length; ++i) {
        nums[i] = i + 1;//from  w w  w.  j  av a2s. c om
    }

    Random generator = new Random();
    for (int i = 0; i < nums.length; ++i) {
        int arrayIndex = generator.nextInt(nums.length - i);
        int tmp = nums[nums.length - 1 - i];
        nums[nums.length - 1 - i] = nums[arrayIndex];
        nums[arrayIndex] = tmp;
    }
    System.out.println(Arrays.toString(nums));
}

From source file:Main.java

public static void main(String[] args) {
    int[][] array = new int[2][10];
    Random rand = new Random();
    for (int i = 0; i < 10; i++) {
        array[0][i] = i;//  w w w . ja v a 2 s .  com
        array[1][i] = (int) rand.nextInt(3) + 1;
    }

    System.out.println(Arrays.deepToString(array));

}

From source file:Main.java

public static void main(String[] args) {
    Random num = new Random();
    int num0, num1, num2, num3, num4, num5, num6, num7;

    num0 = num.nextInt(7) + 1;
    num1 = num.nextInt(8);/*w  ww.  java2s .  co m*/
    num2 = num.nextInt(8);
    num3 = num.nextInt(643) + 101;
    num4 = num.nextInt(10);
    num5 = num.nextInt(10);
    num6 = num.nextInt(10);
    num7 = num.nextInt(10);

    String randnum = "A random phone number: ";
    System.out.print(randnum);
    System.out.print(num0);
    System.out.print(num1);
    System.out.print(num2);
    System.out.print("-" + num3 + "-");
    System.out.print(num4);
    System.out.print(num5);
    System.out.print(num6);
    System.out.println(num7);
}

From source file:Main.java

public static void main(final String[] args) throws Exception {
    Random random = new Random();
    Set<Integer> intSet = new HashSet<>();
    while (intSet.size() < 6) {
        intSet.add(random.nextInt(49) + 1);
    }/*from  w  w  w  . jav a2 s .  c  o m*/
    Integer[] ints = intSet.toArray(new Integer[intSet.size()]);
    System.out.println(Arrays.toString(ints));
}

From source file:MainClass.java

public static void main(String[] args) {
    Random generator = new Random();

    for (int i = 0; i < 10; i++)
        data[i] = 10 + generator.nextInt(90);

    Arrays.sort(data);//  w  w  w.  j a  va  2  s. c  o m

    System.out.println(binarySearch(3));
}

From source file:com.prl.sort.InsertSort.java

/**
 * @param args/*from   w  w  w. j a v a2s  .c  o  m*/
 */
public static void main(String[] args) {
    Integer[] arr = new Integer[10];
    Random random = new Random();
    int index = 0;
    while (true) {
        int num = random.nextInt(100) + 1;
        arr[index++] = num;
        if (index > 9) {
            break;
        }
    }

    System.out.println(StringUtils.join(arr, ","));

    int temp = 0;
    for (int i = 1; i < arr.length; i++) {
        int j = i - 1;
        temp = arr[i]; // ?
        // ???????
        for (; j >= 0 && arr[j] > temp; j--) {
            arr[j + 1] = arr[j];
            arr[j] = temp;
        }
    }

    System.out.println(StringUtils.join(arr, ","));
}

From source file:Main.java

public static void main(String[] args) {

    Random rand = new Random();

    int randnumb;
    int array[] = new int[5];
    for (int j = 0; j <= 4; j++) {
        randnumb = 1 + rand.nextInt(11);
        array[j] = randnumb;// www .  j  ava 2 s.  c  o m
    }
    System.out.println(Arrays.toString(array));

}

From source file:Main.java

public static void main(String[] args) {
    char array[][] = new char[5][5];
    Random rnd = new Random();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            int x = rnd.nextInt(5); // 0 to 4
            switch (x) {
            case 0: {
                array[i][j] = 'a';
                break;
            }//  w w w. jav a  2s  . com
            case 1: {
                array[i][j] = 'b';
                break;
            }
            case 2: {
                array[i][j] = 'c';
                break;
            }
            case 3: {
                array[i][j] = 'd';
                break;
            }
            case 4: {
                array[i][j] = 'e';
                break;
            }
            }
        }
    }
    System.out.println(Arrays.deepToString(array));
}

From source file:Main.java

public static void main(String args[]) {
    int rnd;//  www . jav  a2 s.  c  om
    Random rand = new Random();
    int[] nums = new int[50];

    boolean[] check = new boolean[50];

    for (int k = 0; k < 50; k++) {
        rnd = rand.nextInt(50);
        //check if the check array index has been set
        //if set regenerate since it is duplicate 
        while (check[rnd]) {
            rnd = rand.nextInt(50);
        }
        nums[k] = rnd;
        check[rnd] = true;
    }
    System.out.println(Arrays.toString(nums));

}

From source file:io.druid.query.aggregation.datasketches.quantiles.GenerateTestData.java

public static void main(String[] args) throws Exception {
    Path buildPath = FileSystems.getDefault().getPath("doubles_build_data.tsv");
    Path sketchPath = FileSystems.getDefault().getPath("doubles_sketch_data.tsv");
    BufferedWriter buildData = Files.newBufferedWriter(buildPath, StandardCharsets.UTF_8);
    BufferedWriter sketchData = Files.newBufferedWriter(sketchPath, StandardCharsets.UTF_8);
    Random rand = new Random();
    int sequenceNumber = 0;
    for (int i = 0; i < 20; i++) {
        int product = rand.nextInt(10);
        UpdateDoublesSketch sketch = UpdateDoublesSketch.builder().build();
        for (int j = 0; j < 20; j++) {
            double value = rand.nextDouble();
            buildData.write("2016010101");
            buildData.write('\t');
            buildData.write(Integer.toString(sequenceNumber)); // dimension with unique numbers for ingesting raw data
            buildData.write('\t');
            buildData.write(Integer.toString(product)); // product dimension
            buildData.write('\t');
            buildData.write(Double.toString(value));
            buildData.newLine();/*w  ww. j a v a 2 s  . c  om*/
            sketch.update(value);
            sequenceNumber++;
        }
        sketchData.write("2016010101");
        sketchData.write('\t');
        sketchData.write(Integer.toString(product)); // product dimension
        sketchData.write('\t');
        sketchData.write(Base64.encodeBase64String(sketch.toByteArray(true)));
        sketchData.newLine();
    }
    buildData.close();
    sketchData.close();
}