Generating a random 32 bit hexadecimal value in C

25,610
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.

Share:
25,610

Related videos on Youtube

sachin
Author by

sachin

Updated on September 19, 2020

Comments

  • sachin
    sachin over 3 years

    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);
        }
    
    }
    
    • Hot Licks
      Hot Licks over 12 years
      Why not just call a random number generator? I forget whether C's rand() returns a 64 bit quantity (or fully random 32), but there are others that do. And what does "hexadecimal" have to do with 32 bit numbers?
  • David Heffernan
    David Heffernan over 12 years
    It returns a value in the range 0 to RAND_MAX
  • Mysticial
    Mysticial over 12 years
    @David Heffernan: Correct, which is only guaranteed to be at least 32767.
  • David Heffernan
    David Heffernan over 12 years
    But the implementation you give won't be more random than Sachin's. Although I agree it is much better.
  • Mysticial
    Mysticial over 12 years
    True, you can actually do it with just 3 calls to rand(). But the shift counts and masks will be a bit different.
  • j0h
    j0h over 7 years
    so would x be a uint32_t data type?
  • rew
    rew about 3 years
    IF your rand only gives 15 bit results, it is likely based off a 15 or 16 bit RNG. Then, if you take 4 consecutive values, there are only 2^16 possible 32-bit results that you're going to get. If your RNG is randomly seeded , you're likely to start seeing identical 32-bit numbers after only about 300 (2^8) numbers have been generated. If you just let it run, then you'll get a repeat after 2^14 (because you're using 4 RNG calls and the cycle is a multiple of 4) or 2^16 (if you end up not requesting a divisor of the loop length each iteration.

Related