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(long seed) 

Source Link

Document

Creates a new random number generator using a single long seed.

Usage

From source file:Main.java

public static void main(String args[]) {

    Random randomno = new Random(1234567890987655L);

    // get next next pseudorandom value 
    int value = randomno.nextInt();

    // check the value  
    System.out.println("Value is: " + value);
}

From source file:Main.java

public static void main(String[] args) {
    long[] numbers = new long[] { 1, 2, 3, 4, 5 };
    Random id = new Random(numbers.length);

    String[] name = new String[] { "a", "b", "c", "d", "e" };

    for (int i = 0; i < numbers.length; i++) {
        int randomPosition = id.nextInt(4);
        long temp = numbers[i];
        numbers[i] = numbers[randomPosition];
        numbers[randomPosition] = temp;/*from w w w . ja v a 2s  .  c  om*/
    }

    for (int i = 0; i < name.length; i++) {
        int randomPosition = id.nextInt(4);
        String temp = name[i];
        name[i] = name[randomPosition];
        name[randomPosition] = temp;
    }
    for (int i = 0; i < numbers.length; i++) {
        System.out.println(i + " ID = " + numbers[i] + " and name = " + name[i]);
    }
}

From source file:MainClass.java

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

    System.out.println("First generator:");
    for (int i = 0; i < 10; i++)
        System.out.println(generator.nextInt());

}

From source file:MainClass.java

public static void main(String args[]) {
    String s[] = { "A", "B", "C", "D", "E", "H", "I" };
    List list1 = Arrays.asList(s);
    List list2 = Arrays.asList(s);
    Random rand = new Random(100);
    Collections.shuffle(list1, rand);
    Collections.shuffle(list2, rand);
    System.out.println(list1);//from  ww  w  . ja va 2 s.  c om
    System.out.println(list2);
}

From source file:ShuffleTest.java

public static void main(String args[]) {
    String simpsons[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" };
    List list1 = Arrays.asList(simpsons);
    List list2 = Arrays.asList(simpsons);
    Random rand = new Random(100);
    Collections.shuffle(list1, rand);
    Collections.shuffle(list2, rand);
    System.out.println(list1);/*w w w  .ja  v  a2s .  c  o m*/
    System.out.println(list2);
}

From source file:MainClass.java

public static void main(String args[]) {
    Object array = Array.newInstance(int.class, 3);

    int length = Array.getLength(array);
    Random generator = new Random(System.currentTimeMillis());
    for (int i = 0; i < length; i++) {
        int random = generator.nextInt();
        Array.setInt(array, i, random);
    }//from   w  ww . ja va 2 s  . com

    for (int i = 0; i < length; i++) {
        int value = Array.getInt(array, i);
        System.out.println("Position: " + i + ", value: " + value);
    }
}

From source file:it.units.malelab.sse.Main.java

public static void main(String[] args) throws IOException {
    Random random = new Random(1);
    VirtualMachine vm = new VirtualMachine(4, 4, 400);
    List<Map<Boolean, List<String>>> datasets = new ArrayList<>();
    datasets.add(Util.loadStrings("/home/eric/Documenti/esperimenti/datasets/Bills-Date.txt", random));
    datasets.add(Util.loadStrings("/home/eric/Documenti/esperimenti/datasets/Log-IP.txt", random));
    datasets.add(Util.loadStrings("/home/eric/Documenti/esperimenti/datasets/Twitter-URL.txt", random));

    Evaluator evaluator = new Evaluator(vm, datasets, 1, 10);

    MyGeneticAlgorithm ga = new MyGeneticAlgorithm(new OnePointCrossover<Integer>(), 0.2, new BinaryMutation(),
            0.6, new TournamentSelection(10), evaluator);
    MyGeneticAlgorithm.setRandomGenerator(new JDKRandomGenerator(1));

    List<Chromosome> chromosomes = new ArrayList<>();
    for (int i = 0; i < 2000; i++) {
        chromosomes.add(new OperationsChromosome(evaluator));
    }/*from  w w  w . j  a va  2s.co m*/
    Population population = new ElitisticListPopulation(chromosomes, chromosomes.size(), 0.99);
    Population finalPopulation = ga.evolve(population, new FixedGenerationCount(10000));
    List<Operation> operations = ((OperationsChromosome) finalPopulation.getFittestChromosome())
            .getOperations();
    for (int i = 0; i < operations.size(); i++) {
        System.out.printf("%4d: %s\n", i, operations.get(i));
    }

}

From source file:org.eclipse.swt.snippets.Snippet375.java

public static void main(String[] args) throws Exception {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 375");
    shell.setLayout(new GridLayout(1, false));

    final StringBuilder sb = new StringBuilder();
    final Random random = new Random(2546);
    for (int i = 0; i < 200; i++) {
        sb.append("Very very long text about ").append(random.nextInt(2000)).append("\t");
        if (i % 10 == 0) {
            sb.append("\n");
        }//from w  w  w .j av  a  2s  . c  o  m
    }

    // H SCROLL
    final Label lbl1 = new Label(shell, SWT.NONE);
    lbl1.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false));
    lbl1.setText("Horizontal Scroll");

    final StyledText txt1 = new StyledText(shell, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL);
    txt1.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    txt1.setText(sb.toString());
    txt1.setMouseNavigatorEnabled(true);

    // V_SCROLL
    final Label lbl2 = new Label(shell, SWT.NONE);
    lbl2.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false));
    lbl2.setText("Vertical Scroll");

    final StyledText txt2 = new StyledText(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    txt2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    txt2.setText(sb.toString());
    txt2.setMouseNavigatorEnabled(true);

    // H SCROLL & V_SCROLL
    final Label lbl3 = new Label(shell, SWT.NONE);
    lbl3.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false));
    lbl3.setText("Horizontal and Vertical Scroll");

    final StyledText txt3 = new StyledText(shell, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    txt3.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

    txt3.setText(sb.toString());
    txt3.setMouseNavigatorEnabled(true);

    final Button enableDisableButton = new Button(shell, SWT.PUSH);
    enableDisableButton.setLayoutData(new GridData(GridData.END, GridData.FILL, true, false));
    enableDisableButton.setText("Disable Mouse Navigation");
    enableDisableButton.addListener(SWT.Selection, e -> {
        if (txt3.getMouseNavigatorEnabled()) {
            enableDisableButton.setText("Enable Mouse Navigation");
        } else {
            enableDisableButton.setText("Disable Mouse Navigation");
        }
        txt3.setMouseNavigatorEnabled(!txt3.getMouseNavigatorEnabled());
    });

    // Disabled Scroll at start
    final Label lbl4 = new Label(shell, SWT.NONE);
    lbl4.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false));
    lbl4.setText("No scroll at start");

    final StyledText txt4 = new StyledText(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    final GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
    gd.minimumHeight = 100;
    txt4.setLayoutData(gd);

    txt4.setText("Disabled scroll");
    txt4.setMouseNavigatorEnabled(true);

    // Disabled Scroll
    final Label lbl5 = new Label(shell, SWT.NONE);
    lbl5.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false));
    lbl5.setText("No scroll");

    final StyledText txt5 = new StyledText(shell, SWT.MULTI | SWT.BORDER);
    final GridData gd5 = new GridData(GridData.FILL, GridData.FILL, true, true);
    gd5.minimumHeight = 100;
    txt5.setLayoutData(gd5);

    txt5.setText("No scroll");
    txt5.setMouseNavigatorEnabled(true);

    shell.setSize(800, 600);
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }

    display.dispose();
}

From source file:com.dianping.dpsf.other.echo.EchoClient.java

/**
 * @param args//from w ww.  ja  v  a  2s  . c o  m
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    PigeonClientMock.setServiceAddress("127.0.0.1:20001,127.0.0.1:20002");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("echo-client.xml");
    IEcho s = (IEcho) ctx.getBean("echo");
    //      System.out.println(s.echo("aa"));
    //      Thread.sleep(3000);
    //      System.out.println(s.echo("aa"));
    //      System.out.println("firing");
    Random rnd = new Random(System.currentTimeMillis());

    List<String[]> hostList = new ArrayList<String[]>();
    hostList.add(new String[] { "127.0.0.1", "20001", "1" });
    PigeonClientMock.getSc().onServiceHostChange("http://service.dianping.com/echoService", hostList);

    int i = 0;
    while (i < 100) {
        try {
            System.out.println(s.echo("aa"));
        } catch (Exception e) {
            System.out.println("EEEEEEEEEEEEEEEEEEEEEEE");
        }
        Thread.sleep(2000);
        if (i == 1) {
            System.out.println("+++++++++++++++++++adding 20002++++++++++++++++");
            hostList = new ArrayList<String[]>();
            hostList.add(new String[] { "127.0.0.1", "20001", "1" });
            hostList.add(new String[] { "127.0.0.1", "20002", "1" });
            PigeonClientMock.getSc().onServiceHostChange("http://service.dianping.com/echoService", hostList);
        }
        if (i == 5) {
            System.out.println("+++++++++++++++++++adding 20003++++++++++++++++");
            hostList = new ArrayList<String[]>();
            hostList.add(new String[] { "127.0.0.1", "20001", "1" });
            hostList.add(new String[] { "192.168.32.111", "20003", "1" });
            PigeonClientMock.getSc().onServiceHostChange("http://service.dianping.com/echoService", hostList);
        }
        if (i == 10) {
            System.out.println("--------------------change wt------------");
            PigeonClientMock.getSc().onHostWeightChange("127.0.0.1", 2);
        }
        i++;
    }

}

From source file:Main.java

public static void main(String[] args) {
    int maximum = 100;
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Integer[] oneRow = { 0, 0, 0, 0 };
    String[] headers = { "A", "B", "C", "D" };
    Integer[][] data = { oneRow, oneRow, oneRow, oneRow, oneRow, };
    DefaultTableModel model = new DefaultTableModel(data, headers);
    JTable table = new JTable(model);
    table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum));
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    frame.add(new JScrollPane(table));
    frame.pack();/* w ww .j a  va  2  s .  c  o m*/
    frame.setVisible(true);
    new Thread(new Runnable() {
        @Override
        public void run() {
            Object waiter = new Object();
            synchronized (waiter) {
                int rows = model.getRowCount();
                int columns = model.getColumnCount();
                Random random = new Random(System.currentTimeMillis());
                boolean done = false;
                while (!done) {
                    int row = random.nextInt(rows);
                    int column = random.nextInt(columns);
                    Integer value = (Integer) model.getValueAt(row, column);
                    value++;
                    if (value <= maximum) {
                        model.setValueAt(value, row, column);
                        try {
                            waiter.wait(15);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    done = true;
                    for (row = 0; row < rows; row++) {
                        for (column = 0; column < columns; column++) {
                            if (!model.getValueAt(row, column).equals(maximum)) {
                                done = false;
                                break;
                            }
                        }
                        if (!done) {
                            break;
                        }
                    }
                }
            }
        }
    }).start();
}