简体   繁体   中英

How to create random numbers a specific number of times?

How can i create a random number a specific numbers of time?

public class Feld  {
    public static void main(String[] args) {
        double k = (int)(Math.random()*1000001);
        int n = 1000000;
        int arr[] = new int[n];
        int i = 0;
        for(i = 0;i<n;i++){
            arr[i] = i;
        }
        boolean found = false;
        i=0;
        while (i < arr.length) {
            if (arr[i] == k) {
                found = true;
                break;
            }
            i++;
        }
        if (found) {
            i++;
            System.out.println(i);
        } 
        else {
            System.out.println((arr.length + 1));
        }
    }
}

My problem is, that if i put k into a loop to create it more than one time i'll get an error at:

if (arr[i] == k)

..I just found out that i made a mistake explaining my problem. The array should be filled with values from 0-1.000.000 and i am supposed to print out the position of a random generated number for a specific amount of times.

If you want to have an array full of random numbers, I suggest using the following:

int n = 1000000;
int arr[] = new int[n];
for(int i = 0; i < n; i++){
    arr[i] = (int)(Math.random() * 1000001);
}

That will work and you don't even need the variable k .


Edit:

If you want to print at what position you find a specific value (for example x = 543 ), you can use the following code:

int x = 543;
int n = 1000000;
int arr[] = new int[n];
for(int i = 0; i < n; i++){
    arr[i] = (int)(Math.random() * 1000001);
    if(arr[i] == x) {
        System.out.println(i);
        break;
    }
}

Edit2

One possible solution to your new problem looks like this:

public class Feld  {
    public static void main(String[] args) {
        int n = 1000000;
        int arr[] = new int[n];
        int i = 0;
        for(i = 0; i < n; i++){
            arr[i] = i; //Filling array with values 0-1000000
        }
        int number = 20;    //Print out position of a random generated number a specific amount of times
        int randomNumber = (int)(Math.random()*1000001); //The random number

        for(int j = 0; j < number; j++) { //Find number for a specific amount of times 
            for(int k = 0; k < arr.length; k++) { //Find number in array
                if(arr[k] == randomNumber) { 
                    System.out.println(arr[k]); //Print
                    break; //Number found, don't have to search anymore
                }
            }
        }
    }
}

I would write a method that returns an array of random numbers and takes an int argument that defines the length of the array.

One possible solution is this:

public static int[] createRandomArray(int length) {
    // create an array of the given length
    int[] result = new int[length];
    // and use a single for loop that puts random int values into every index
    for (int i = 0; i < result.length; i++) {
        result[i] = ThreadLocalRandom.current().nextInt();
    }
    // then simply return the result
    return result;
}

Try it as follows

public static void main(String[] args) {
    // super primitive time measurement:
    // take the moment in time before calling the method
    Instant start = Instant.now();
    // then call the method
    int[] array = createRandomArray(1000000);
    // and take the moment in time after the method returned
    Instant end = Instant.now();
    // then calculate the duration
    Duration duration = Duration.between(start, end);
    // and print the duration in milliseconds
    System.out.printf("Array creation took %d milliseconds\n", duration.toMillis());
}

The result is the following output on my system:

Array creation took 10 milliseconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM