Generate N Random Hexadecimal Numbers - GeeksforGeeks

02 Dec, 2021
Given a positive integer N, the task is to generate N random hexadecimal integers.
Examples:
Input: N = 3Output:F9AD0D9E19B24CD01A5E
Approach: The given problem can be solved with the help of the rand() function which is used to generate random integers. A character array can be created which stores all the possible characters in the hexadecimal notation and randomly select the characters from the array.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Maximum length of the random integerconst int maxSize = 10; // Function to generate N Hexadecimal// integersvoid randomHexInt(int N){    srand(time(0));     // Stores all the possible characters    // in the Hexadecimal notation    char hexChar[]        = { '0', '1', '2', '3', '4', '5',            '6', '7', '8', '9', 'A', 'B',            'C', 'D', 'E', 'F' };     // Loop to print N integers    for (int i = 0; i < N; i++) {         // Randomly select length of the        // int in the range [1, maxSize]        int len = rand() % maxSize + 1;         // Print len characters        for (int j = 0; j < len; j++) {             // Print a randomly selected            // character            cout << hexChar[rand() % 16];        }        cout << '\n';    }} // Driver Codeint main(){    int N = 3;    randomHexInt(N);     return 0;}
// Java program for the above approachclass GFG{     // Maximum length of the random integerstatic int maxSize = 10; // Function to generate N Hexadecimal// integersstatic void randomHexInt(int N){         // Stores all the possible characters    // in the Hexadecimal notation    char hexChar[]        = { '0', '1', '2', '3', '4', '5',            '6', '7', '8', '9', 'A', 'B',            'C', 'D', 'E', 'F' };     // Loop to print N integers    for (int i = 0; i < N; i++) {         // Randomly select length of the        // int in the range [1, maxSize]        int len = (1 + (int)(Math.random() * 100)) % maxSize + 1;         // Print len characters        for (int j = 0; j < len; j++) {             // Print a randomly selected            // character            System.out.print(hexChar[(1 + (int)(Math.random() * 100)) % 16]);        }        System.out.println();    }}  // Driver Codepublic static void main (String[] args){    int N = 3;    randomHexInt(N);}} // This code is contributed by sanjoy_62.
# Python3 program for the above approachimport random,math # Maximum length of the random integermaxSize = 10; # Function to generate N Hexadecimal# integersdef randomHexInt(N) :     # Stores all the possible characters    # in the Hexadecimal notation    hexChar = [ '0', '1', '2', '3', '4', '5',            '6', '7', '8', '9', 'A', 'B',            'C', 'D', 'E', 'F'];     # Loop to print N integers    for i in range(N) :         # Randomly select length of the        # int in the range [1, maxSize]        Len = math.floor(random.random() * (maxSize - 1) + 1)         # Print len characters        for j in range(Len) :             # Print a randomly selected            # character            print(hexChar[math.floor(random.random() * 16)],end = "");         print(); # Driver Codeif __name__ == "__main__" :     N = 3;    randomHexInt(N);     # This code is contributed by AnkThon
// C# program for the above approachusing System;using System.Collections.Generic;using System.Linq; public class GFG {     // Maximum length of the random integerstatic int maxSize = 10; // Function to generate N Hexadecimal// integersstatic void randomHexInt(int N){         // Stores all the possible characters    // in the Hexadecimal notation    char[] hexChar        = { '0', '1', '2', '3', '4', '5',            '6', '7', '8', '9', 'A', 'B',            'C', 'D', 'E', 'F' };                 // For random generator    Random rand = new Random();     // Loop to print N integers    for (int i = 0; i < N; i++) {         // Randomly select length of the        // int in the range [1, maxSize]        int len = rand.Next() % maxSize + 1;         // Print len characters        for (int j = 0; j < len; j++) {             // Print a randomly selected            // character            Console.Write(hexChar[rand.Next() % 16]);        }        Console.WriteLine();    }} // Driver Codepublic static void Main (string[] args) {         int N = 3;    randomHexInt(N);}} // This code is contributed by code_hunt.
<script>        // JavaScript Program to implement        // the above approach         // Maximum length of the random integer        const maxSize = 10;         // Function to generate N Hexadecimal        // integers        function randomHexInt(N) {             // Stores all the possible characters            // in the Hexadecimal notation            let hexChar                = ['0', '1', '2', '3', '4', '5',                    '6', '7', '8', '9', 'A', 'B',                    'C', 'D', 'E', 'F'];             // Loop to print N integers            for (let i = 0; i < N; i++) {                 // Randomly select length of the                // int in the range [1, maxSize]                let len = Math.random() * (maxSize - 1) + 1;                 // Print len characters                for (let j = 0; j < len; j++) {                     // Print a randomly selected                    // character                    document.write(hexChar[Math.floor(Math.random() * (16))]);                }                document.write("<br>")            }        }         // Driver Code        let N = 3;        randomHexInt(N); // This code is contributed by Potta Lokesh    </script>
B71C3
EC3BBC90
82410C0D
 
Time Complexity: O(N*M) where M represents the maximum length of the random string.Auxiliary Space: O(1)
lokeshpotta20
ankthon
simmytarika5
sanjoy_62
code_hunt
number-theory
TrueGeek-2021
Mathematical
Strings
TrueGeek
number-theory
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to convert a given number to words
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Program to multiply two matrices
Count ways to reach the n'th stair
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Python program to check if a string is palindrome or not
Different methods to reverse a string in C/C++