C language has a built-in function to generate random numbers. In this program, we will use that function to generate a few random numbers.

Problem Definition

The program uses the \Large random() function from C standard library and generates random numbers. Its only requirement is a max limit to the random numbers generated. Therefore, the random number is between \Large 0 to max which is supplied by the user. Also, we keep the \Large random() function inside of a loop so that this process is repeated for \Large n times. The value of \Large n is input by the user.

Program Source Codes

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int n, max, num, c;

    printf("Enter Number of Random Numbers You Want:");
    scanf("%d", &n);

    printf("Enter Maximum value for the Random Numbers:");
    scanf("%d", &max);

    printf("%d random numbers from 0 to %d\n\n",n,max);

for(c = 1; c <= n; c++)
{

    num = rand() % max + 1;
    printf("%d\n",num);

}

system("PAUSE"); 
return 0;

}
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    int n, max, num;

    cout << "Enter Number of Random Numbers You Want: ";
    cin >> n;

    cout << "Enter Maximum value for the Random Numbers: ";
    cin >> max;

    cout << "\n" << n << " random numbers from 1 to " << max << "\n\n";

    srand(time(0));   // seed the random number generator

    for (int c = 1; c <= n; c++) {
        num = rand() % max + 1;
        cout << num << endl;
    }

    return 0;
}
import java.util.Random;
import java.util.Scanner;

class RandomNumbers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random rand = new Random();

        System.out.print("Enter Number of Random Numbers You Want: ");
        int n = sc.nextInt();

        System.out.print("Enter Maximum value for the Random Numbers: ");
        int max = sc.nextInt();

        System.out.println("\n" + n + " random numbers from 1 to " + max + "\n");

        for (int c = 1; c <= n; c++) {
            int num = rand.nextInt(max) + 1;
            System.out.println(num);
        }

        sc.close();
    }
}
import random

n = int(input("Enter Number of Random Numbers You Want: "))
max_val = int(input("Enter Maximum value for the Random Numbers: "))

print(f"\n{n} random numbers from 1 to {max_val}\n")

for _ in range(n):
    num = random.randint(1, max_val)
    print(num)

Output

Enter Number of Random Numbers You Want: 10
Enter Maximum value for the Random Numbers: 1000
10 random numbers from 0 to 1000

42
468
335
501
170
725
479
359
963
465
Press any key to continue . . . _