jeudi 22 janvier 2015

Random Class java

I've been doing some practice exercises on the use of the Random class.


I have a class called Card, which has five instance variables containing a reference to an integer which should sit within a specified range.


Those five numbers are being generated using the random class. Code is as follows:



public class Card
{

private String cardName;
private int yearOfInvention;
private int novelty;
private int popularity;
private double trendiness;
private int numberOfDialects;


public Card(String cardName) {
this.cardName = cardName;
Random rand = new Random();
yearOfInvention = 1900 + rand.nextInt(111);
novelty = rand.nextInt(10);
popularity = rand.nextInt(100);
trendiness = rand.nextDouble() + rand.nextInt(4);
numberOfDialects = rand.nextInt(50);
}


For 'trendiness', my value needs to be any digit between 0-5, including fractional parts, but only to one decimal point.


Currently it would give me e.g.


private double trendiness 1.2784547479963435


Is there a way of limiting the number of decimal points?





Aucun commentaire:

Enregistrer un commentaire