Thread: Creating Random hex numbers

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by disruptor108 View Post
    7fff
    Ok well that confirms your assumption about the value of RAND_MAX, and the print function is the problem. I would try some version of sprintf(). So instead of:
    Code:
    >_tcscpy(ProductID, _T("{%8X-%4X-%4X-%4X-%12X}", rand(), rand(), rand(), rand(), rand()));
    Try:
    Code:
    _tsprintf(ProductID, _T("{%8X-%4X-%4X-%4X-%12X}", rand(), rand(), rand(), rand(), rand()));
    And if that doesn't work or there is no such animal, then try sprintf():
    Code:
    _sprintf(ProductID, "{%8X-%4X-%4X-%4X-%12X}", rand(), rand(), rand(), rand(), rand()));

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And if you're creating a GUID, use the WinAPI function CornedBee suggests.

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    12
    Personally I'm very much not a fan of CoCreateGuid, instead, i initialize a variable to the size of _GUID and fill it with CryptGenRandom. Advertising my MAC address is not something I'm willing to do in the name of uniqueness.

    Ehtyar.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Ehtyar View Post
    Personally I'm very much not a fan of CoCreateGuid, instead, i initialize a variable to the size of _GUID and fill it with CryptGenRandom. Advertising my MAC address is not something I'm willing to do in the name of uniqueness.

    Ehtyar.
    Then don't call it GUID
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    12
    I didn't. I also believe the topic for this thread is "Creating Random hex numbers", to which my suggestion is perfectly suited.

    Ehtyar.

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Ehtyar View Post
    Personally I'm very much not a fan of CoCreateGuid, instead, i initialize a variable to the size of _GUID and fill it with CryptGenRandom. Advertising my MAC address is not something I'm willing to do in the name of uniqueness.

    Ehtyar.
    I strongly suggest you rethink that strategy. Using a different algorithm to everyone else gives a possibility that the GUIDs you pick will clash with GUIDs created by others, and even potentially with your own.
    The idea you speak of means that you can no longer call them GUIDs, as they are not GLOBALLY unique. They are in fact only LOCALLY unique, with very high probability.

    Your escuse about not advertising your MAC address is absurdly paranoid.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #22
    Registered User
    Join Date
    Nov 2006
    Posts
    12
    Can i remind you again that i made no mention of GUID, other than using the structure, because i like the format nor did i ever claim that my idea would generate "globally uniqe" anything. Can i also remind you again that this topic is called "Creating Random hex numbers", and that the code put forth in the initial post is even more likely to generate collisions than my own.

    Ehtyar.

  8. #23
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    OK, true...I said I was trying to create random hex numbers, but yes...more specifically I was trying to create a random GUID for an installation program that I had created.

    I was wondering...what header file needs to be included to use CoCreateGUID?

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think <ole2.h>, but MSDN will tell you for sure. Just search for the function and look at the bottom of its page. It tells you what header file to include and what library to link against.
    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

  10. #25
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    Thanks a lot. You guys answered my problem

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, 11: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