I have 3 instructions-
1- Use rand() to generate a number less then 100 and greater than 1
2- Call the prime() function to test the result
3- Print out the result

So far i have found how to get a prime number but don't know how to fill up the conditions.(how to use rand() function)

#include <iostream>
using namespace std;

int smallFactor(int x) {
   int answer = 2;
   while (x % answer != 0) answer++;
   return answer;
}

int main() {
   int n;
   cout << "Enter a number greater than 1: " << endl;
   cin >> n;
   if (n<1 ||n>99) cout<<"Illegal";
   else if (n == smallFactor(n)) cout << "That is prime.";
   else cout << "That is not prime.";
   cout << endl;
   return 0;
}

Dani AI

Generated

Below are short, practical examples and a few tips to address the original goal: pick a random integer in the range 2..99 and print whether it is prime. This avoids the infinite-loop pitfall in the OP's smallFactor approach, uses an efficient trial-division test (only odd divisors up to sqrt(n)), and uses a modern RNG instead of reseeding incorrectly each time. Credit to and for the seeding hint, and to and for the divisor/sqrt guidance.

#include <iostream>
#include <random>

bool is_prime(int n) {
    if (n <= 1) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
    for (int d = 3; d * d <= n; d += 2)
        if (n % d == 0) return false;
    return true;
}

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist(2, 99);

    int n = dist(gen);
    std::cout << n << " is " << (is_prime(n) ? "prime" : "not prime") << '\n';
}
import random, math

def is_prime(n):
    if n <= 1: return False
    if n == 2: return True
    if n % 2 == 0: return False
    r = int(math.isqrt(n))
    for d in range(3, r + 1, 2):
        if n % d == 0: return False
    return True

n = random.randint(2, 99)
print(n, "is prime" if is_prime(n) else "is not prime")

Quick notes and troubleshooting:

  • Do not seed the generator inside a loop; seed once at program start. That was the likely cause if you saw the same number repeated.
  • Using rand()%range can introduce slight bias; for simple tasks it is usually fine, but std::uniform_int_distribution is preferred.
  • Always check n <= 1 before testing; the original smallFactor loop will never terminate for n == 1.
  • For much larger numbers use a probabilistic test (Miller–Rabin) or a sieve if you need many primes — for 2..99 the trial-division above is plenty.

Recommended Answers

All 8 Replies

You say rand() and it returns a random number between 0 and RAND_MAX.

That's how you use it.

You can call srand(something) to initialize the random number generator -- where something is a relatively unpredictable value, such as the current time.

>>while (x % answer != 0) answer++;
1) The code may be short but gives more work to the computer. You have to check if x is divisible by a number lesser than its square root and thats all you have to do. So In this case the while loop need not execute more than 10 times at the maximum.
2) What will happen if the input is 1?

i need it to create a random number and to print out whether this random number is prime or not!! On tutorial there is noting like that :(

If you are having problem with rand() go google 'rand cpp reference'. It has a nice example as to how to use it.

And like the above poster has mentioned,

>>while (x % answer != 0) answer++;

is not a good practice. Since you will check 2, then 3, then 4 then 5 then 6 and so forth until you reach the number. And once you know it is not divisible by 2, it's also not divisible by 4 or 6 or any multiples of 2 (which comes out to be even numbers).

A tutorial isn't generally going to tell you how to make the program you want to make :P . Here's a simple prime function

bPrime = true;
for (int ii(2); ii < _iVal / 2; ii++)
{
    if (!(_iVal % ii))
        {
             bPrime = false;	
             break;
        }
}

Where iVal is the number you are checking against. Here is a random number function:

int iRand = rand() % MAX;

Note it's unseeded. If i remember correctly to seed it you just use srand(INT_VALUE) . Usually this is done with the time (ie clock()) to make a quite random pseudo-random number. I'm gonna let you put the 2 ideas together yourself, so you at least learn something.

Another solution
int Prime(int k)
{
int x;
for(int i=1;i<k;i++)
if(k%i==0) x++;
return x;
}

in main method u have to write
int 9;
if(prime(9)>1)
cout<<"9 is prime ";
else cout<<"not prime";

Another solution
int Prime(int k)
{
int x;
for(int i=1;i<k;i++)
if(k%i==0) x++;
return x;
}

in main method u have to write
int 9;
if(prime(9)>1)
cout<<"9 is prime ";
else cout<<"not prime";

That's an ok solution, but can be optimized for efficiency in 2 ways - you need only mod up to (k / 2) in your loop. Any number larger than half of k can't be multiplied by any integer to equal k, so the last k / 2 calculations will always result in a remainder. Also, it should break out as soon as the number is found to be not prime, otherwise your just wasting clock cycles calculating what you already know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.