Program to generate Random number in a given Range

Description

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
To generate random number between given range both in c
and java follow the program.
To generate random number between given range both in c and java follow the program.
To generate random number between given range both in c 
and java follow the program.

C/C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* C Program to generate Random number in a given Range */
//Save it as GenerateRandomNumber.c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int i, countNum, lower, upper;
printf("Enter the number of random number you want : ");
scanf("%d",&countNum);
printf("Enter the lower limit : ");
scanf("%d",&lower);
printf("Enter the upper limit : ");
scanf("%d",&upper);
//To genearate new set of numbers everytime use srand funtion
/*By using srand function current time is used which will always be
different so everytime a different set of numbers is generated.*/
srand(time(0));
printf("The numbers in a given range\n");
for(i=0;i<countNum;i++){
//To genearate new set of numbers everytime use srand funtion.
//After rerun program, only same set of numbers will be generated everytime.
//To genearate new set everytime use srand funtion.
int number = (rand() % (upper - lower + 1)) + lower;
printf("%d ", number);
}
return 0;
}
/* C Program to generate Random number in a given Range */ //Save it as GenerateRandomNumber.c #include<stdio.h> #include<stdlib.h> #include<time.h> int main(){ int i, countNum, lower, upper; printf("Enter the number of random number you want : "); scanf("%d",&countNum); printf("Enter the lower limit : "); scanf("%d",&lower); printf("Enter the upper limit : "); scanf("%d",&upper); //To genearate new set of numbers everytime use srand funtion /*By using srand function current time is used which will always be different so everytime a different set of numbers is generated.*/ srand(time(0)); printf("The numbers in a given range\n"); for(i=0;i<countNum;i++){ //To genearate new set of numbers everytime use srand funtion. //After rerun program, only same set of numbers will be generated everytime. //To genearate new set everytime use srand funtion. int number = (rand() % (upper - lower + 1)) + lower; printf("%d ", number); } return 0; }
/* C Program to generate Random number in a given Range */
//Save it as GenerateRandomNumber.c

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){

    int i, countNum, lower, upper;

    printf("Enter the number of random number you want : ");
    scanf("%d",&countNum);

    printf("Enter the lower limit : ");
    scanf("%d",&lower);

    printf("Enter the upper limit : ");
    scanf("%d",&upper);

    //To genearate new set of numbers everytime use srand funtion
    /*By using srand function current time is used which will always be
      different so everytime a different set of numbers is generated.*/
    srand(time(0));

    printf("The numbers in a given range\n");
    for(i=0;i<countNum;i++){
        //To genearate new set of numbers everytime use srand funtion.
        //After rerun program, only same set of numbers will be generated everytime.
        //To genearate new set everytime use srand funtion.
        int number = (rand() % (upper - lower + 1)) + lower;
        printf("%d ", number);
    }

    return 0;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Input:
Enter the number of random number you want : 3
Enter the lower limit : 1
Enter the upper limit : 10
Output:
The numbers in a given range
8 6 5
Input: Enter the number of random number you want : 3 Enter the lower limit : 1 Enter the upper limit : 10 Output: The numbers in a given range 8 6 5
Input:
Enter the number of random number you want : 3
Enter the lower limit : 1
Enter the upper limit : 10

Output:
The numbers in a given range
8 6 5

Java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* Java Program to generate Random number in a given Range */
//Save it as GenerateRandomNumber.java
import java.io.*;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class GenerateRandomNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i, countNum, lower, upper, number;
System.out.println("Enter the number of random number you want : ");
countNum = scanner.nextInt();
System.out.println("Enter the lower limit : ");
lower = scanner.nextInt();
System.out.println("Enter the upper limit : ");
upper = scanner.nextInt();
System.out.println("The numbers in a given range");
//Generating new set of number between lower and upper limit
for(i=0;i<countNum;i++) {
number = ThreadLocalRandom.current().nextInt(lower, upper + 1);
System.out.println(number);
}
}
}
/* Java Program to generate Random number in a given Range */ //Save it as GenerateRandomNumber.java import java.io.*; import java.util.Scanner; import java.util.concurrent.ThreadLocalRandom; public class GenerateRandomNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i, countNum, lower, upper, number; System.out.println("Enter the number of random number you want : "); countNum = scanner.nextInt(); System.out.println("Enter the lower limit : "); lower = scanner.nextInt(); System.out.println("Enter the upper limit : "); upper = scanner.nextInt(); System.out.println("The numbers in a given range"); //Generating new set of number between lower and upper limit for(i=0;i<countNum;i++) { number = ThreadLocalRandom.current().nextInt(lower, upper + 1); System.out.println(number); } } }
/* Java Program to generate Random number in a given Range */
//Save it as GenerateRandomNumber.java

import java.io.*;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class GenerateRandomNumber {

    public static void main(String[] args) {

            Scanner scanner = new Scanner(System.in);
        
            int i, countNum, lower, upper, number;

        System.out.println("Enter the number of random number you want : ");
        countNum = scanner.nextInt();

        System.out.println("Enter the lower limit : ");
        lower = scanner.nextInt();

        System.out.println("Enter the upper limit : ");
        upper = scanner.nextInt();
        
            System.out.println("The numbers in a given range");
            //Generating new set of number between lower and upper limit
        for(i=0;i<countNum;i++) {
        	
        	number = ThreadLocalRandom.current().nextInt(lower, upper + 1);
        	
        	System.out.println(number);
        }
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Input:
Enter the number of random number you want :
3
Enter the lower limit :
1
Enter the upper limit :
10
Output:
The numbers in a given range
8
6
5
Input: Enter the number of random number you want : 3 Enter the lower limit : 1 Enter the upper limit : 10 Output: The numbers in a given range 8 6 5
Input:
Enter the number of random number you want : 
3
Enter the lower limit : 
1
Enter the upper limit : 
10

Output:
The numbers in a given range
8
6
5

Related Programs

1) Program to Check Whether a Number is Prime or Not
2) Program to Check Whether a Number is Even or Odd
3) Check whether a given number is a perfect number or not
4) Program to find all prime numbers in given range
5) Program to find the average of numbers in a given range
6) Program to print all Squares of numbers from 1 to given Range
7) Program to calculate Gross Salary
8) Program to calculate percentage mark of student
9) Program to reverse a number
10) Program to display multiplication table of a number
Share Me

Leave a Reply