vendredi 26 août 2016

Java Math.abs random vs. nextInt()

Creating a random number generator in Java - looking at num1 and num2, in which instances would I use either of these methods for creating a random number between 1 and 8? Is one more efficient than the other, any other benefits?

import java.util.*;

public class Chpt3 {
  public static void main(String[] args) {
    Random rand = new Random();
    int num1 = Math.abs(rand.nextInt()) % 8 + 1;
    int num2 = rand.nextInt(8) + 1;
    System.out.println(num1);
    System.out.println(num2);

  }
}




Aucun commentaire:

Enregistrer un commentaire