Example usage for java.util Random Random

List of usage examples for java.util Random Random

Introduction

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

Prototype

public Random() 

Source Link

Document

Creates a new random number generator.

Usage

From source file:Main.java

public static void main(String[] args) {
    BigInteger bigInteger = new BigInteger("2000000000000");// uper limit
    BigInteger min = new BigInteger("1000000000");// lower limit
    BigInteger bigInteger1 = bigInteger.subtract(min);
    Random rnd = new Random();
    int maxNumBitLength = bigInteger.bitLength();

    BigInteger aRandomBigInt;/*from  w w  w. j  ava 2  s.com*/

    aRandomBigInt = new BigInteger(maxNumBitLength, rnd);
    if (aRandomBigInt.compareTo(min) < 0)
        aRandomBigInt = aRandomBigInt.add(min);
    if (aRandomBigInt.compareTo(bigInteger) >= 0)
        aRandomBigInt = aRandomBigInt.mod(bigInteger1).add(min);

    System.out.println(aRandomBigInt);
}

From source file:Main.java

License:asdf

public static void main(String[] args) {
    int howManyWords = 2;
    List<String> listOfWords = new ArrayList<>();
    Random random = new Random();
    listOfWords.addAll(Arrays.asList(randomMessages));
    List<String> selectedRandomMessages = new ArrayList<>();
    for (int i = 0; i < howManyWords; i++) {
        int randomNumber = random.nextInt(listOfWords.size());
        String randomItem = listOfWords.get(randomNumber);
        selectedRandomMessages.add(randomItem);
        listOfWords.remove(randomItem);/*w  w  w  . j a v  a  2  s. c o  m*/
    }
    System.out.println(selectedRandomMessages);
}

From source file:Main.java

public static void main(final String[] args) throws Exception {
    Random RND = new Random();
    ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    List<Future<String>> results = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        results.add(es.submit(new TimeSliceTask(RND.nextInt(10), TimeUnit.SECONDS)));
    }//from w ww  .  j a va2  s.c o m
    es.shutdown();
    while (!results.isEmpty()) {
        Iterator<Future<String>> i = results.iterator();
        while (i.hasNext()) {
            Future<String> f = i.next();
            if (f.isDone()) {
                System.out.println(f.get());
                i.remove();
            }
        }
    }
}

From source file:Main.java

public static void main(String args[]) {
    // create Linked List
    List<Integer> list = new LinkedList<Integer>();

    // populate list
    list.add(5);//from  ww w.jav a 2 s  .c  om
    list.add(2);
    list.add(1);
    list.add(-3);

    System.out.println("List before shuffle: " + list);

    // shuffle the list
    Collections.shuffle(list, new Random());

    System.out.println("List after shuffle: " + list);
}

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

/**
 * @param args/*  w  w  w.j  a v a  2  s.  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:HashCollider.java

/**i
 * @param args the command line arguments
 *//*  ww w.j a v a 2  s.  co m*/
public static void main(String[] args) {
    Random random = new Random();
    byte[] nbytes1 = new byte[64];
    byte[] nbytes2 = new byte[64];
    float average = 0;
    // set number of bits for checking collision
    int number_of_bits = 4;
    random.nextBytes(nbytes1);
    random.nextBytes(nbytes2);

    String sha1 = DigestUtils.sha256Hex(nbytes1);
    String sha2 = DigestUtils.sha256Hex(nbytes2);

    String nsh1 = new BigInteger(sha1, 16).toString(2);
    String nsh2 = new BigInteger(sha2, 16).toString(2);
    int i = 0;
    for (int j = 0; j < 5; j++) {
        while (true) {
            if (nsh1.substring(0, number_of_bits - 1).equals(nsh2.substring(0, number_of_bits - 1))) {
                System.out.println(i);
                average += i;
                break;
            }
            i++;
            //            System.out.println("......"+i);
            random.nextBytes(nbytes1);
            random.nextBytes(nbytes2);
            sha1 = DigestUtils.sha256Hex(nbytes1);
            sha2 = DigestUtils.sha256Hex(nbytes2);
            nsh1 = new BigInteger(sha1, 16).toString(2);
            nsh2 = new BigInteger(sha2, 16).toString(2);
        }
        i = 0;
        //        System.out.println(nsh1+"\n"+nsh2);
        random.nextBytes(nbytes1);
        random.nextBytes(nbytes2);
        sha1 = DigestUtils.sha256Hex(nbytes1);
        sha2 = DigestUtils.sha256Hex(nbytes2);
        nsh1 = new BigInteger(sha1, 16).toString(2);
        nsh2 = new BigInteger(sha2, 16).toString(2);

    }
    //
    System.out.println("Average for  " + number_of_bits + " bits is  " + average / 5);

}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button1 = new JButton("Select Me");
    final JButton button2 = new JButton("No Select Me");
    final Random random = new Random();

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton button = (JButton) actionEvent.getSource();
            int red = random.nextInt(255);
            int green = random.nextInt(255);
            int blue = random.nextInt(255);
            button.setBackground(new Color(red, green, blue));
        }//  ww w.jav  a 2 s . c om
    };

    PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
            String property = propertyChangeEvent.getPropertyName();
            if ("background".equals(property)) {
                button2.setBackground((Color) propertyChangeEvent.getNewValue());
            }
        }
    };

    button1.addActionListener(actionListener);
    button1.addPropertyChangeListener(propertyChangeListener);
    button2.addActionListener(actionListener);

    frame.add(button1, BorderLayout.NORTH);
    frame.add(button2, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument doc = new DefaultStyledDocument();
    JTextPane textPane = new JTextPane(doc);
    textPane.setText("this is a test.");

    Random random = new Random();
    for (int i = 0; i < textPane.getDocument().getLength(); i++) {
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set,
                new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        StyleConstants.setFontSize(set, random.nextInt(12) + 12);
        StyleConstants.setBold(set, random.nextBoolean());
        StyleConstants.setItalic(set, random.nextBoolean());
        StyleConstants.setUnderline(set, random.nextBoolean());

        doc.setCharacterAttributes(i, 1, set, true);
    }//from  ww w  .j  a  v  a  2s  .c  o m

    frame.add(new JScrollPane(textPane));
    frame.setSize(500, 400);
    frame.setVisible(true);
}

From source file:MathOps.java

public static void main(String[] args) {
    // Create a random number generator,
    // seeds with current time by default:

    Random rand = new Random();

    int i, j, k;//from ww  w  . ja  v  a  2s . c  o  m

    // Choose value from 1 to 100:

    j = rand.nextInt(100) + 1;

    k = rand.nextInt(100) + 1;

    printInt("j", j);
    printInt("k", k);

    i = j + k;
    printInt("j + k", i);

    i = j - k;
    printInt("j - k", i);

    i = k / j;
    printInt("k / j", i);

    i = k * j;
    printInt("k * j", i);

    i = k % j;
    printInt("k % j", i);

    j %= k;
    printInt("j %= k", j);

    // Floating-point number tests:

    float u, v, w; // applies to doubles, too

    v = rand.nextFloat();

    w = rand.nextFloat();

    printFloat("v", v);
    printFloat("w", w);

    u = v + w;
    printFloat("v + w", u);

    u = v - w;
    printFloat("v - w", u);

    u = v * w;
    printFloat("v * w", u);

    u = v / w;
    printFloat("v / w", u);

    // the following also works for

    // char, byte, short, int, long,

    // and double:

    u += v;
    printFloat("u += v", u);

    u -= v;
    printFloat("u -= v", u);

    u *= v;
    printFloat("u *= v", u);

    u /= v;
    printFloat("u /= v", u);

}

From source file:PropertyChangeListenerDemo.java

public static void main(String args[]) {
    Runnable runner = new Runnable() {
        public void run() {
            JFrame frame = new JFrame("Button Sample");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final JButton button1 = new JButton("Select Me");
            final JButton button2 = new JButton("No Select Me");
            final Random random = new Random();
            // Define ActionListener
            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JButton button = (JButton) actionEvent.getSource();
                    int red = random.nextInt(255);
                    int green = random.nextInt(255);
                    int blue = random.nextInt(255);
                    button.setBackground(new Color(red, green, blue));
                }/*w  w w.ja v a 2s  .com*/
            };
            // Define PropertyChangeListener
            PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
                    String property = propertyChangeEvent.getPropertyName();
                    if ("background".equals(property)) {
                        button2.setBackground((Color) propertyChangeEvent.getNewValue());
                    }
                }
            };
            button1.addActionListener(actionListener);
            button1.addPropertyChangeListener(propertyChangeListener);
            button2.addActionListener(actionListener);
            frame.add(button1, BorderLayout.NORTH);
            frame.add(button2, BorderLayout.SOUTH);
            frame.setSize(300, 100);
            frame.setVisible(true);
        }
    };
    EventQueue.invokeLater(runner);
}