public void click_random_retailer() 
    int randomNumber = new Random().nextInt(discoverPageObjects.merchantDivs.size() - 1) + 1;
    WebElement div = discoverPageObjects.merchantDivs.get(randomNumber);
    retailerName = div.findElement(By.cssSelector(discoverPageObjects.merchantNameLoc)).getText();
    System.out.println("Going to click Merchant ->  " + retailerName);
    WebElement merchantBtn = div.findElement(By.cssSelector(discoverPageObjects.merchantBtnLoc));

1 Answer 1


No. From the Java documentation:

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

As an aside, the range of the parameterless version of nextInt is Integer.MIN_VALUE to Integer.MAX_VALUE, inclusive.

Proof that it can return negative numbers:

$ cat r.java
import java.util.Random;

public class r {
    public static void main(final String[] args) {
        final Random random = new Random();
        for (int i=0; i<100; i++) {
$ javac -d . r.java
$ java -cp . r | grep -- - | wc -l

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.