dimanche 4 octobre 2015

How to place value at random locations in an array using Math.random?

i have an array called Events.

String[] Events = new String[20];

How can I write a method to place 10 random events into this array using math.random() only? My attempt to solving the question is this: I'm new and still learning moving from python to java, stress much.

public static void AddRandomEvents(Events[] event) {
    for(int i = 0; i < event.length; i++){
        int rand = (int) (Math.random() + 10) + 1;
        System.out.println(event[rand]);
    }
}

I have a class called Events that's why i used Events[] here

class Events {
private String StartingTime;
private String EventName;
private int EventPriority;


public Events(String time, String name, int priority){
    this.StartingTime = time;
    this.EventName = name;
    this.EventPriority = (int)(Math.random() *3) + 1;;
}

public String toString()
{
    return "Time of Event: " + StartingTime + " " + "Event Name: " + EventName + " " + "Priority: " + " " + EventPriority;
}

}

Any help or contribution would be helpful. Thanks.




Aucun commentaire:

Enregistrer un commentaire