mercredi 18 mai 2016

Auto-generated sample list doesn't contain random elements [duplicate]

This question already has an answer here:

I am currently writing a method that creates sample objects used for testing.

My code is similar to the following:

List<Person> employeeList = new ArrayList<>();
Person employee = new Person();

String [] names = {"John", "Jack", "Linda", "Nancy"}; 
int choice;

int employeeCount = randomInt(0, 4);
for(int i=0; i<employeeCount; i++) {

    choice = randomInt(0, 4);
    employee.setId(String.valueOf(randomInt(100000, 1000000))); // 6-digit id between 100000 and 999999
    employee.setName(names[choice]);
    employeeList.add(employee);
}

The randomInt method is as follows:

import java.util.Random;
.
.
// Returns any value from minValue to maxValue - 1
public static int randomInt(int minValue, int maxValue) {

    Random rand = new Random();
    int num = rand.nextInt(maxValue - minValue);

    return minValue + num;
}

The idea is to generate valid random data and create objects.

However, I can't get it to input random data. Either it's all 'Jack's or all 'Nancy's or such.

I don't get it. Is my random number generator faulty? I've tried to change it multiple times and didn't get any improvement.




Aucun commentaire:

Enregistrer un commentaire