简体   繁体   English

在 Java 中生成随机素数

[英]Generating a random prime number in Java

I am trying to make a program that generates a pseudo random number and checks if it is prime.我正在尝试制作一个生成伪随机数并检查它是否为素数的程序。 Then the program will loop until the random number is a prime.然后程序将循环直到随机数是素数。 However, it does not always print a prime number.但是,它并不总是打印质数。

  public class Prime_Number_Generator {

            public static void main(String[] args) {

                int[] primesList = {2, 3, 5, 7, 11, 13, 17, 19}; // list of known primes
                int num = 0;
                int i = 0;
                int counter = 1;
                Random rand = new Random(); // generate a random number


                while (i != counter) {          
                    num = rand.nextInt(1000) + 1;

                    if (num % primesList[i] == 0)  // check if num is evenly divisible by a prime from the list
                       i++;

                    }
                    else { // if it is prime exit loop
                        i = 0;
                        counter = 0;
                    }
                 }
                 System.out.println(num);  // print the number

    }
}

This is how I would do it, however be aware that if you input large numbers into this isPrime method it will start to lag.这就是我的做法,但是请注意,如果您将大量数字输入到此isPrime方法中,它将开始滞后。 Whenever you have an error in your code try unit testing (testing small parts of code).每当您的代码出现错误时,请尝试单元测试(测试代码的一小部分)。 That way you will be able to find and fix your code errors easily, this is why I would highly recommend using some type of isPrime method, rather than having all the code in the main method.这样你就可以很容易地找到并修复你的代码错误,这就是为什么我强烈建议使用某种类型的isPrime方法,而不是将所有代码都放在main方法中。

public class Prime_Number_Generator {
    public static void main(String[] args) {
        int num = 0;
        Random rand = new Random(); // generate a random number
        num = rand.nextInt(1000) + 1;

        while (!isPrime(num)) {          
            num = rand.nextInt(1000) + 1;
        }
        System.out.println(num);  // print the number
    }

    /**
     * Checks to see if the requested value is prime.
     */
    private static boolean isPrime(int inputNum){
        if (inputNum <= 3 || inputNum % 2 == 0) 
            return inputNum == 2 || inputNum == 3; //this returns false if number is <=1 & true if number = 2 or 3
        int divisor = 3;
        while ((divisor <= Math.sqrt(inputNum)) && (inputNum % divisor != 0)) 
            divisor += 2; //iterates through all possible divisors
        return inputNum % divisor != 0; //returns true/false
    }
}

I think there is a bit of an easier method to generate prime numbers though this one is a little bit slow I think this might help:我认为有一种更简单的方法来生成素数,虽然这个方法有点慢,但我认为这可能会有所帮助:

public class Prime_number_generator
{
    public static int main()
    {
    int prime;
        while (true)
        {
            int count = 0;
            double x  = Math.random();
            double y  = 10000 * x;
            double z  = Math.ceil(y);
            prime     = (int)z;
            for (int i = 1; i <= prime; i++)
            {
                int modfactor = prime % i;
                if (modfactor == 0)
                {
                    count++;
                }
            }
            if (count == 2)
            {
                break;
            }
        }
        return prime;
    }
}

I hope this is useful to you.我希望这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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