Generating a random 32 bit hexadecimal value in C

1

What would be the best way to generate a random 32-bit hexadecimal value in C? In my current implementation I am generating each bit separately but the output is not completely random ... many values are repeated several times. Is it better to generate the entire random number instead of generating each bit separately?

The random number should make use of the entire 32 bit address space (0x00000000 to 0xffffffff)

file = fopen(tracefile,"wb"); // create file
for(numberofAddress = 0; numberofAddress<10000; numberofAddress++){ //create 10000 address
    if(numberofAddress!=0)
        fprintf(file,"\n"); //start a new line, but not on the first one

    fprintf(file, "0 ");
    int space;

    for(space = 0; space<8; space++){ //remove any 0 from the left
        hexa_address = rand() % 16;
        if(hexa_address != 0){
            fprintf(file,"%x", hexa_address);
            space++;
            break;
        }
        else if(hexa_address == 0 && space == 7){ //in condition of 00000000
            fprintf(file,"%x", "0");
            space++;
        }
    }

    for(space; space<8; space++){ //continue generating the remaining address
        hexa_address = rand() % 16;
        fprintf(file,"%x", hexa_address);
    }

}
c
random
hex
asked on Stack Overflow Oct 1, 2011 by sachin

3 Answers

10
x = rand() & 0xff;
x |= (rand() & 0xff) << 8;
x |= (rand() & 0xff) << 16;
x |= (rand() & 0xff) << 24;

return x;

rand() doesn't return a full random 32-bit integer. Last time I checked it returned between 0 and 2^15. (I think it's implementation dependent.) So you'll have to call it multiple times and mask it.

answered on Stack Overflow Oct 1, 2011 by Mysticial
0

Do this way.It creates a bigger number than the earlier logic .If you are interested the MSB then the below logic is good .:

/** x = rand() ^ rand()<<1; **/

#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <string>
#include <stdio.h>

int main () {
    int i, n;

    n = 50;
    uint x,y ;
    //4294967295 :UNIT_MAX
    /* Intializes random number generator */
    srand((unsigned) time(0));

    for( i = 0 ; i < n ; i++ ) {

        /**WAY 1 **/
        x = rand() ^ rand()<<1;

        printf("x:%u\t",x);
        printf("Difference1:(4294967295 - %u) = %u\n",x,(4294967295 - x));

        /**WAY 2 **/
        y  = rand() & 0xff;
        y |= (rand() & 0xff) << 8;
        y |= (rand() & 0xff) << 16;
        y |= (rand() & 0xff) << 24;
        printf("y:%u\t",y);
        printf("Difference2:(4294967295 - %u) = %u\n",y,(4294967295 - y));

        printf("Difference between two is = %u\n",(x) - (y));


    }
    printf("End\n");

    return(0);
}
answered on Stack Overflow Sep 19, 2019 by jailaxmi k
-1

You can just create any random number that's at least 32 bit wide and format that as hex. Examples:

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

uint32_t n;

n = mrand48();    // #1
n = rand();       // #2

FILE * f = fopen("/dev/urandom", "rb");
fread(&n, sizeof(uint32_t), 1, f);  // #3

// ... etc. etc. E.g. Windows Crypto API

char hex[9];
sprintf(hex, "%08X", n);

Now hex is a string containing eight random hexadecimal digits. Don't forget to seed the various pseudo random number generators (using srand48() and srand(), respectively, for #1 and #2). Since you'll essentially have to seed the PRNGs from random source with at least one 32-bit integer, you might as well tap the random source directly (unless you're using time() or something "non-random" like that).

answered on Stack Overflow Oct 1, 2011 by Kerrek SB

User contributions licensed under CC BY-SA 3.0