Program to generate a random single digit number Last Updated : 18 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Write a program to generate a random single-digit number. Example 1: 7Example 2: 3 Approach: To solve the problem, follow the below idea: To generate a random single-digit number, we can first generate a random integer and then take apply modulo to get the last digit of that random integer. This last digit can be the random single digit number. Step-by-step algorithm: Generate a random integer.Take modulo 10 of the random integer to get a single digit random number.Below is the implementation of the algorithm: C++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); int randomDigit = rand() % 10; cout << randomDigit << endl; return 0; } C #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); int randomDigit = rand() % 10; printf("%d\n", randomDigit); return 0; } Java import java.util.Random; public class RandomDigit { public static void main(String[] args) { Random random = new Random(); int randomDigit = random.nextInt(10); System.out.println(randomDigit); } } Python3 import random random_digit = random.randint(0, 9) print(random_digit) C# using System; class Program { static void Main() { Random random = new Random(); int randomDigit = random.Next(0, 10); Console.WriteLine(randomDigit); } } JavaScript const randomDigit = Math.floor(Math.random() * 10); console.log(randomDigit); Output8 Time Complexity: O(1) as the time complexity of generating a random number is constant.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to generate a random single digit number S srinam Follow Improve Article Tags : DSA Similar Reads Program to generate a random two-digit number Write a program to generate a random two-digit number. Example 1: 73Example 2: 26 Approach: To solve the problem, follow the below idea: We can generate a random two-digit number by generating a random integer first and then modulo it by 90. Now, after modulo 90 the remainder can be in the range 0 t 2 min read Program to generate a random three digit even number Write a program to generate a random three-digit even number. Examples: Example 1: 482Example 2: 736 Approach: To solve the problem, follow the below idea: We can generate a random three-digit number by generating a random integer first and then modulo it by 900. Now, after modulo with 900, the rema 3 min read Program to generate a random number between L to R Write a program that generates a random number within a specified range [L, R]. The program should take two integers, L and R, as input and output a random number between L and R (inclusive). Examples: Input: L = 10, R = 20Output: 15 Input: L = -5, R = 5Output: 3 Approach: To solve the problem, foll 2 min read Program to generate random string in PHP Given a size N and the task is to generate a random string of size N. Examples: Input: 5Output: eR3DsInput: 10Output: MPRCyBgdcnUsing a Domain String and Random IndexCreate a domain string that contains small letters, capital letters, and the digits (0 to 9). Then generate a random number pick the c 2 min read R Program to Generate a Random Password Password generation is a common task in programming languages. It is required for security applications and various accounts managing systems. A random password is not easily guessable which also improves the security of the accounts systems with the aim to protect information. In R Programming Lang 4 min read Like