Mikkel
Mikkel

Reputation: 1811

Generate 100 random numbers between 14-24 in java and place in byte array

i'm trying to make a method which can generate 100 random numbers between 14-24, and place it in my byte array before i'm going to send it to a server.

My code so far looks like this:

private byte data[] = new byte[100];

public void generateData() {
    int min = 14;
    int max = 24;

    for (int i = 0; i < 100; i++) {

        data[i] 

    }

}

So for every step in the array, it should put in a byte number between 14-24.. but when i'm trying to use the Math.random, it only works with double.

Upvotes: 0

Views: 284

Answers (2)

Arvind
Arvind

Reputation: 1237

int x = (int)(Math.random()*100000000l); 
int randomNo = x%14+14;

Upvotes: 0

DeiAndrei
DeiAndrei

Reputation: 947

You can use the Random class. Generate a random number between 0 and 10 and add 14 to it.

Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(11) + 14;

Upvotes: 1

Related Questions