Example usage for com.google.gwt.user.client Random nextInt

List of usage examples for com.google.gwt.user.client Random nextInt

Introduction

In this page you can find the example usage for com.google.gwt.user.client Random nextInt.

Prototype

public static native int nextInt(int upperBound) ;

Source Link

Document

Returns a random int between 0 (inclusive) and upperBound (exclusive) with roughly equal probability of returning any particular int in this range.

Usage

From source file:gwt.material.demo.errai.client.page.addins.table.service.FakePersonService.java

License:Apache License

@Override
public void getPeople(int startIndex, int viewSize, List<String> categories, AsyncCallback<People> async) {
    List<Person> flatData = new ArrayList<>();
    if (categories == null) {
        // Load all data
        for (String category : FakePersonService.categories) {
            flatData.addAll(peopleMap.get(category));
        }//w w  w  . j a v a2s . c  o m
    } else {
        // Load data by categories
        for (String category : categories) {
            for (Person person : peopleMap.get(category)) {
                flatData.add(person);
            }
        }
    }

    People people = new People();
    for (int i = startIndex; i < (startIndex + viewSize); i++) {
        try {
            people.add(flatData.get(i));
        } catch (IndexOutOfBoundsException e) {
            // ignored.
        }
    }
    people.setAbsoluteTotal(flatData.size());
    // Fake a delay for the demo
    new Timer() {
        @Override
        public void run() {
            async.onSuccess(people);
        }
    }.schedule(Math.min(200, Random.nextInt(500)));
}

From source file:gwt.material.demo.errai.client.page.addins.table.service.FakePersonService.java

License:Apache License

@Override
public void getCategories(AsyncCallback<List<String>> async) {
    // Fake a delay for the demo
    new Timer() {
        @Override/* w ww.  jav a 2  s .co m*/
        public void run() {
            async.onSuccess(categories);
        }
    }.schedule(Math.min(200, Random.nextInt(500)));
}

From source file:gwtquery.plugins.draggable.client.GWTIntegrationSample.java

License:Apache License

/**
 * Create a Dynamic tree. The code comes from the GWT show case :
 * http://gwt.google.com/samples/Showcase/Showcase.html#!CwTree
 *
 * @return//from  w w w .j a  v  a  2s  .  com
 */
private Widget createDynamicTree() {
    // Create a new tree
    Tree dynamicTree = new Tree();

    // Add some default tree items
    for (int i = 0; i < 5; i++) {
        TreeItem item = dynamicTree.addItem("Item " + i);

        // Temporarily add an item so we can expand this node
        item.addItem("");
    }

    // Add a handler that automatically generates some children
    dynamicTree.addOpenHandler(new OpenHandler<TreeItem>() {
        public void onOpen(OpenEvent<TreeItem> event) {
            TreeItem item = event.getTarget();
            if (item.getChildCount() == 1) {
                // Close the item immediately
                item.setState(false, false);

                // Add a random number of children to the item
                String itemText = item.getText();
                int numChildren = Random.nextInt(5) + 2;
                for (int i = 0; i < numChildren; i++) {
                    TreeItem child = item.addItem(itemText + "." + i);
                    child.addItem("");
                }

                // Remove the temporary item when we finish loading
                item.getChild(0).remove();

                // Reopen the item
                item.setState(true, false);
            }
        }
    });

    // Return the tree (decorated)
    DecoratorPanel decPanel = new DecoratorPanel();
    decPanel.setWidget(dynamicTree);
    return decPanel;
}

From source file:gwtquery.plugins.droppable.client.contactcellsample.ContactDatabase.java

License:Apache License

/**
 * Create a new random {@link ContactInfo}.
 * // w  w w  .jav a  2 s . c o  m
 * @return the new {@link ContactInfo}.
 */
@SuppressWarnings("deprecation")
private ContactInfo createContactInfo() {
    // set all created contact in OTHERS category
    ContactInfo contact = new ContactInfo(Category.OTHERS);
    contact.setLastName(nextValue(LAST_NAMES));
    if (Random.nextBoolean()) {
        // Male.
        contact.setFirstName(nextValue(MALE_FIRST_NAMES));
    } else {
        // Female.
        contact.setFirstName(nextValue(FEMALE_FIRST_NAMES));
    }

    // Create a birthday between 20-80 years ago.
    int year = (new Date()).getYear() - 21 - Random.nextInt(61);
    contact.setBirthday(new Date(year, Random.nextInt(12), 1 + Random.nextInt(31)));

    // Create an address.
    int addrNum = 1 + Random.nextInt(999);
    String addrStreet = nextValue(STREET_NAMES);
    String addrSuffix = nextValue(STREET_SUFFIX);
    contact.setAddress(addrNum + " " + addrStreet + " " + addrSuffix);
    return contact;
}

From source file:gwtquery.plugins.droppable.client.gfindersample.FileSystem.java

License:Apache License

/**
 * Generate contents for the directory <code>root</code>
 * /* ww w.  j  a v  a2 s  . co m*/
 * @param root
 * @param fileNbr
 * @param subDirNbr
 */
private void generateDirContent(File root, int fileNbr, int subDirNbr) {

    assert root.getType() == FileType.DIRECTORY;

    ListDataProvider<File> files = new ListDataProvider<File>();

    for (int i = 0; i < subDirNbr; i++) {
        File f = new File(FileType.DIRECTORY);
        f.setName(root.getName() + "Event" + i);
        f.setParent(root);
        files.getList().add(f);

        generateDirContent(f, Math.max(5, Random.nextInt(MAX_FILE_NBR)), 0);
    }

    for (int i = 0; i < fileNbr; i++) {
        File f = new File(FileType.FILE);
        f.setName(root.getName() + "PICT" + i + ".jpg");
        f.setParent(root);
        files.getList().add(f);
    }

    filesByDirectory.put(root, files);

}

From source file:gwtquery.plugins.droppable.client.gfindersample.FileSystem.java

License:Apache License

/**
 * Generate the file system/*  www .j a  v  a2 s.c o  m*/
 * 
 * @param rootDirectory
 */
private void generateDirTree(File rootDirectory) {
    // generate user directories
    ListDataProvider<File> files = new ListDataProvider<File>();

    for (int i = 0; i < Math.max(10, Random.nextInt(20)); i++) {
        File f = new File(FileType.DIRECTORY);
        f.setName(nextValue(USER_DIRECTORY_NAME));
        f.setParent(rootDirectory);
        files.getList().add(f);
        // generate Event sub directories
        generateDirContent(f, 0, Math.max(5, Random.nextInt(MAX_DIR_NBR)));

    }
    filesByDirectory.put(rootDirectory, files);

}

From source file:gwtquery.plugins.gwtcaptcha.client.Captcha.java

License:Apache License

private int rnd(int n) {
    return n > 0 ? Random.nextInt(n) : Random.nextInt();
}

From source file:java.math.Primality.java

License:Apache License

/**
 * A random number is generated until a probable prime number is found.
 * /* w  w  w .  jav a  2s.  c  o m*/
 * @see BigInteger#BigInteger(int,int,Random)
 * @see BigInteger#probablePrime(int,Random)
 * @see #isProbablePrime(BigInteger, int)
 */
static BigInteger consBigInteger(int bitLength, int certainty, Random rnd) {
    // PRE: bitLength >= 2;
    // For small numbers get a random prime from the prime table
    if (bitLength <= 10) {
        int rp[] = offsetPrimes[bitLength];
        return BIprimes[rp[0] + rnd.nextInt(rp[1])];
    }
    int shiftCount = (-bitLength) & 31;
    int last = (bitLength + 31) >> 5;
    BigInteger n = new BigInteger(1, last, new int[last]);

    last--;
    do {// To fill the array with random integers
        for (int i = 0; i < n.numberLength; i++) {
            n.digits[i] = rnd.nextInt();
        }
        // To fix to the correct bitLength
        n.digits[last] |= 0x80000000;
        n.digits[last] >>>= shiftCount;
        // To create an odd number
        n.digits[0] |= 1;
    } while (!isProbablePrime(n, certainty));
    return n;
}

From source file:jdramaix.client.ContactDatabase.java

License:Apache License

/**
 * Create a new random {@link ContactInfo}.
 *
 * @return the new {@link ContactInfo}./*w  ww . j a va2  s  .c  om*/
 */
@SuppressWarnings("deprecation")
private ContactInfo createContactInfo() {
    ContactInfo contact = new ContactInfo();
    contact.setLastName(nextValue(LAST_NAMES));
    if (Random.nextBoolean()) {
        // Male.
        contact.setFirstName(nextValue(MALE_FIRST_NAMES));
    } else {
        // Female.
        contact.setFirstName(nextValue(FEMALE_FIRST_NAMES));
    }

    // Create a birthday between 20-80 years ago.
    int year = (new Date()).getYear() - 21 - Random.nextInt(61);
    contact.setBirthday(new Date(year, Random.nextInt(12), 1 + Random.nextInt(31)));

    // Create an address.
    int addrNum = 1 + Random.nextInt(999);
    String addrStreet = nextValue(STREET_NAMES);
    String addrSuffix = nextValue(STREET_SUFFIX);
    contact.setAddress(addrNum + " " + addrStreet + " " + addrSuffix);
    return contact;
}

From source file:net.husoftware.client.spike.DragDropTree.java

License:Open Source License

@Override
public void onModuleLoad() {
    Tree tree = new Tree();

    RootPanel.get("main").add(tree);

    // root is not draggable.
    TreeItem treeItem = new TreeItem(new DragDropLabel("root", false, true));
    tree.addItem(treeItem);/* ww  w.j  av  a2s  . com*/

    // Add some folders
    treeItem.addItem(new DragDropLabel("folder1", true, true));
    treeItem.addItem(new DragDropLabel("folder2", true, true));

    TreeItem folder3 = treeItem.addItem(new DragDropLabel("folder3", true, true));
    folder3.addItem(new DragDropLabel("folder3-1", true, true));
    folder3.addItem(new DragDropLabel("folder3-2", true, true));

    // Add some leaves to the tree
    List<TreeItem> stack = new ArrayList<TreeItem>();
    stack.add(tree.getItem(0));

    int filenum = 1;
    while (!stack.isEmpty()) {
        TreeItem item = stack.remove(0);
        for (int i = 0; i < item.getChildCount(); i++) {
            stack.add(item.getChild(i));
        }

        int files = Random.nextInt(4) + 1;
        for (int j = 0; j < files; j++) {
            item.addItem(new TreeItem(new DragDropLabel("File " + filenum, true, false)));
            filenum++;
        }
        item.setState(true);
    }
}