Creating Random hex numbers

This is a discussion on Creating Random hex numbers within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a random product ID for a project I'm developing. I'm just looking to create some random ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    17

    Post Creating Random hex numbers

    I'm trying to create a random product ID for a project I'm developing. I'm just looking to create some random hex values. However, I noticed that when I used %x with rand(), it generated 8 digit hex values when I know the maximum value of rand() is 32767.

    I have a code snippet...am I doing anything wrong?

    Code:
    srand(time(NULL));
    _tcscpy(ProductID, _T("{%8X-%4X-%4X-%4X-%12X}", rand(), rand(), rand(), rand(), rand()));
    _tprintf(ProductID);
    
    Output: {2102E416-3388587B-1C7A6E8-7FFDD000-    CCCCCCCC}

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you only want the first four digits, you can do it via a bitwise AND operation.

    Code:
    _tcscpy(ProductID, _T("{%8X-%4X-%4X-%4X-%12X}", rand(), rand() & 0xFFFF, rand() & 0xFFFF, rand() & 0xFFFF, rand()));

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    Why do I get a 8 digit hex value when the maximum value is only 0x7FFF?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    In all honesty, I don't think you should if you're using the rand() from stdlib.h/cstdlib.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Just out of curiosity, what does this print?
    Code:
    _tprintf("%d\n", RAND_MAX);

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    Code:
    _tprintf(_T("%d",RAND_MAX));
    
    Output: 2020380200

    ??? This doesn't make much sense to me...

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What about:
    Code:
    std::cout << RAND_MAX << std::endl;
    See what that prints.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You know what.... I wonder.....

    Could you be using the _T macro all wrong, and those large digits are the address of a new string it's creating? Those addresses possibly point to your random numbers.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what is _T()?

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Match parens.
    Code:
    _tcscpy(ProductID, _T("{%8X-%4X-%4X-%4X-%12X}", rand(), rand(), rand(), rand(), rand()));
    Are you missing some intervening call in haste?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> what is _T()?
    It's a macro that converts the string inside to either a wide character string or a regular string depending on whether UNICODE is defined for your project or not.

    AFAIK it does not take format parameters like that, although I might be wrong.

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    Quote Originally Posted by swoopy View Post
    What about:
    Code:
    std::cout << RAND_MAX << std::endl;
    See what that prints.
    7fff

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    Quote Originally Posted by MacGyver View Post
    You know what.... I wonder.....

    Could you be using the _T macro all wrong, and those large digits are the address of a new string it's creating? Those addresses possibly point to your random numbers.
    I believe you're right MacGyver...

    Code:
    srand(time(NULL));
    _tcscpy(ProductID, _T("{%8X-%4X-%4X-%4X-%12X}\n", 12345678, rand(), rand(), rand(), rand()));
    _tprintf(ProductID);
    
    Output: {7DDC7E09-3459C061-1C7A77E-7FFDB000-    CCCCCCCC}
    What would be a better way to do this then?

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You shouldn't create GUIDs with such a poor RNG as the stdlib rand() anyway. The WinAPI provides a CreateGUID() function that creates a good GUID.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,452
    Even if the rand was good - the output cannot be named GUID... GUID creation algorithm uses some data about creation date, computer hardware and maybe something else to ensure the uniqueness of the return value during the Universe life... So the algorithm using only some pseudo-random generator will be wrong in any case, because it cannot guarantee such a uniqueness
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Generatin Random Numbers
    By dantu1985 in forum C Programming
    Replies: 15
    Last Post: 08-13-2007, 01:21 AM
  3. Creating a UDF to create random numbers
    By rgmills in forum C++ Programming
    Replies: 5
    Last Post: 11-15-2005, 10:22 PM
  4. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21