mercredi 3 février 2021

SOLVED:Why is my code generating the same numbers from random [closed]

When I create a new object in a different Class, the code works fine, but the creation of a second object does not give a different number(idNumber). Why is this happening?

  Random generator= new Random();
public Vehicle(int arrival) {
    idNumber = generator.nextInt(99)+1;}

That is the code for the constructor^. Below is the code creating the objects.

Vehicle car1 = new Vehicle(2);
Vehicle car2 = new Vehicle(3);

Object "car2" will have the same idNumber as "car1", how can I fix this?




Aucun commentaire:

Enregistrer un commentaire