Example usage for java.lang Math random

List of usage examples for java.lang Math random

Introduction

In this page you can find the example usage for java.lang Math random.

Prototype

public static double random() 

Source Link

Document

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0 .

Usage

From source file:com.tohours.imo.util.TohoursUtils.java

/**
 * ???/*from  ww  w  .  j a  v a2s  .  c  om*/
 * 
 * @param sLen
 * @return
 */
public static String randomKey(int sLen) {
    String base;
    String temp;
    int i;
    int p;

    base = "1234567890abcdefghijklmnopqrstuvwxyz";
    temp = "";
    for (i = 0; i < sLen; i++) {
        p = (int) (Math.random() * 37);
        if (p > 35)
            p = 35;
        temp += base.substring(p, p + 1);
    }
    return temp;
}

From source file:org.openhie.openempi.service.MailEngineTest.java

public void testSendMessageWithAttachment() throws Exception {
    final String ATTACHMENT_NAME = "boring-attachment.txt";

    //mock smtp server
    Wiser wiser = new Wiser();
    int port = 2525 + (int) (Math.random() * 100);
    mailSender.setPort(port);//  w  ww.  j a va  2  s  . c o m
    wiser.setPort(port);
    wiser.start();

    Date dte = new Date();
    String emailSubject = "grepster testSendMessageWithAttachment: " + dte;
    String emailBody = "Body of the grepster testSendMessageWithAttachment message sent at: " + dte;

    ClassPathResource cpResource = new ClassPathResource("/test-attachment.txt");
    mailEngine.sendMessage(new String[] { "foo@bar.com" }, mailMessage.getFrom(), cpResource, emailBody,
            emailSubject, ATTACHMENT_NAME);

    wiser.stop();
    assertTrue(wiser.getMessages().size() == 1);
    WiserMessage wm = wiser.getMessages().get(0);
    MimeMessage mm = wm.getMimeMessage();

    Object o = wm.getMimeMessage().getContent();
    assertTrue(o instanceof MimeMultipart);
    MimeMultipart multi = (MimeMultipart) o;
    int numOfParts = multi.getCount();

    boolean hasTheAttachment = false;
    for (int i = 0; i < numOfParts; i++) {
        BodyPart bp = multi.getBodyPart(i);
        String disp = bp.getDisposition();
        if (disp == null) { //the body of the email
            Object innerContent = bp.getContent();
            MimeMultipart innerMulti = (MimeMultipart) innerContent;
            assertEquals(emailBody, innerMulti.getBodyPart(0).getContent());
        } else if (disp.equals(Part.ATTACHMENT)) { //the attachment to the email
            hasTheAttachment = true;
            assertEquals(ATTACHMENT_NAME, bp.getFileName());
        } else {
            fail("Did not expect to be able to get here.");
        }
    }
    assertTrue(hasTheAttachment);
    assertEquals(emailSubject, mm.getSubject());
}

From source file:com.beetle.framework.util.OtherUtil.java

/**
 * Generates pseudo-random long from specific range. Generated number is
 * great or equals to min parameter value and less then max parameter value.
 * /*from   w  ww. j a  va 2 s .co  m*/
 * @param min
 *            lower (inclusive) boundary
 * @param max
 *            higher (exclusive) boundary
 * @return pseudo-random value
 */

public static long randomLong(long min, long max) {
    return min + (long) (Math.random() * (max - min));
}

From source file:ThreadDemo.java

/** This is the dummy method our threads all call */
static synchronized void compute() {
    // Figure out how many times we've been called by the current thread
    Integer n = (Integer) numcalls.get();
    if (n == null)
        n = new Integer(1);
    else/*from  ww  w . j av a  2  s  .  c  om*/
        n = new Integer(n.intValue() + 1);
    numcalls.set(n);

    // Display the name of the thread, and the number of times called
    System.out.println(Thread.currentThread().getName() + ": " + n);

    // Do a long computation, simulating a "compute-bound" thread
    for (int i = 0, j = 0; i < 1000000; i++)
        j += i;

    // Alternatively, we can simulate a thread subject to network or I/O
    // delays by causing it to sleep for a random amount of time:
    try {
        // Stop running for a random number of milliseconds
        Thread.sleep((int) (Math.random() * 100 + 1));
    } catch (InterruptedException e) {
    }

    // Each thread politely offers the other threads a chance to run.
    // This is important so that a compute-bound thread does not "starve"
    // other threads of equal priority.
    Thread.yield();
}

From source file:ar.org.neuroph.core.Weight.java

/**
 * Sets random weight value
 */
public void randomize() {
    this.value = Math.random() - 0.5d;
}

From source file:com.pivotal.gfxd.demo.controller.DemoController.java

@RequestMapping(value = "/random", produces = "application/json", method = RequestMethod.GET)
@ResponseBody//from www. j av  a  2 s. c  o  m
public ResponseEntity<TimestampValue> getRandom() {
    float rate = (float) ((Math.random() - 0.5) * 1000 + 5000);

    TimestampValue tv = new TimestampValue("event", System.currentTimeMillis() / 1000, rate);

    return new ResponseEntity(tv, HttpStatus.OK);
}

From source file:eu.databata.engine.util.PropagatorLock.java

private String generateToken() {
    return StringUtils.abbreviate("" + Math.random(), 200);
}

From source file:ListIt.java

public ListIt() {
    JFrame f = new JFrame();
    final PartsListModel pcm = new PartsListModel();
    ListCellRenderer lcr = new MyLabelRenderer();
    JList jl = new JList(pcm);
    jl.setCellRenderer(lcr);/*w w  w .  j  a va  2 s  . c om*/
    ListSelectionModel lsm = jl.getSelectionModel();
    lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jl.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                String element[] = (String[]) pcm.getElementAt(e.getFirstIndex());
                System.out.println(element[0] + " : " + element[1] + " : " + element[2]);
            }
        }
    });
    JScrollPane jsp = new JScrollPane(jl);
    JComboBox jc = new JComboBox(pcm);
    jc.setRenderer(lcr);
    JButton jb = new JButton("Add Merchandise");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            pcm.addElement(partsList[(int) (Math.random() * partsList.length)]);
        }
    });
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.NORTH);
    c.add(jc, BorderLayout.CENTER);
    c.add(jb, BorderLayout.SOUTH);
    f.setSize(250, 250);
    f.show();
}

From source file:edu.ucla.stat.SOCR.chart.demo.LineChartDemo3.java

/**
 * creaets XYDataset/*from   ww  w .j  a v  a2s  . com*/
 *  @param isDemo true use the demo data, false use data from the JTable
 *  @return XYDataset
 */
protected XYDataset createDataset(boolean isDemo) {
    if (isDemo) {
        XYSeriesCollection dataset = new XYSeriesCollection();
        for (int i = 0; i < 4; i++) {
            XYSeries series = new XYSeries("S" + i);
            for (int j = 0; j < 4; j++) {
                series.add(j, Math.random() * 100);
            }
            dataset.addSeries(series);
        }
        return dataset;
    } else
        return super.createDataset(false);
}