Generate N Random Hexadecimal Numbers
Last Updated :
24 Mar, 2023
Given a positive integer N, the task is to generate N random hexadecimal integers.
Examples:
Input: N = 3
Output:
F9AD0D9
E19B24CD01
A5E
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++
#include <bits/stdc++.h>
using namespace std;
const int maxSize = 10;
void randomHexInt( int N)
{
srand ( time (0));
char hexChar[]
= { '0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'A' , 'B' ,
'C' , 'D' , 'E' , 'F' };
for ( int i = 0; i < N; i++) {
int len = rand () % maxSize + 1;
for ( int j = 0; j < len; j++) {
cout << hexChar[ rand () % 16];
}
cout << '\n' ;
}
}
int main()
{
int N = 3;
randomHexInt(N);
return 0;
}
|
Java
class GFG{
static int maxSize = 10 ;
static void randomHexInt( int N)
{
char hexChar[]
= { '0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'A' , 'B' ,
'C' , 'D' , 'E' , 'F' };
for ( int i = 0 ; i < N; i++) {
int len = ( 1 + ( int )(Math.random() * 100 )) % maxSize + 1 ;
for ( int j = 0 ; j < len; j++) {
System.out.print(hexChar[( 1 + ( int )(Math.random() * 100 )) % 16 ]);
}
System.out.println();
}
}
public static void main (String[] args)
{
int N = 3 ;
randomHexInt(N);
}
}
|
Python3
import random,math
maxSize = 10 ;
def randomHexInt(N) :
hexChar = [ '0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'A' , 'B' ,
'C' , 'D' , 'E' , 'F' ];
for i in range (N) :
Len = math.floor(random.random() * (maxSize - 1 ) + 1 )
for j in range ( Len ) :
print (hexChar[(math.floor(random.random() * 16 ))],end = "");
print ();
if __name__ = = "__main__" :
N = 3 ;
randomHexInt(N);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
static int maxSize = 10;
static void randomHexInt( int N)
{
char [] hexChar
= { '0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'A' , 'B' ,
'C' , 'D' , 'E' , 'F' };
Random rand = new Random();
for ( int i = 0; i < N; i++) {
int len = rand.Next() % maxSize + 1;
for ( int j = 0; j < len; j++) {
Console.Write(hexChar[rand.Next() % 16]);
}
Console.WriteLine();
}
}
public static void Main ( string [] args) {
int N = 3;
randomHexInt(N);
}
}
|
Javascript
<script>
const maxSize = 10;
function randomHexInt(N) {
let hexChar
= [ '0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'A' , 'B' ,
'C' , 'D' , 'E' , 'F' ];
for (let i = 0; i < N; i++) {
let len = Math.random() * (maxSize - 1) + 1;
for (let j = 0; j < len; j++) {
document.write(hexChar[(Math.floor(Math.random() * (16)))]);
}
document.write( "<br>" )
}
}
let N = 3;
randomHexInt(N);
</script>
|
Output:
B71C3
EC3BBC90
82410C0D
Time Complexity: O(N*M) where M represents the maximum length of the random string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...