# Creating Random hex numbers

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-04-2007
disruptor108
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}```
• 06-04-2007
MacGyver
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()));`
• 06-04-2007
disruptor108
Why do I get a 8 digit hex value when the maximum value is only 0x7FFF?
• 06-04-2007
MacGyver
In all honesty, I don't think you should if you're using the rand() from stdlib.h/cstdlib.
• 06-04-2007
swoopy
Just out of curiosity, what does this print?
Code:

`_tprintf("%d\n", RAND_MAX);`
• 06-04-2007
disruptor108
Code:

```_tprintf(_T("%d",RAND_MAX)); Output: 2020380200```

??? This doesn't make much sense to me...
• 06-04-2007
swoopy
Code:

`std::cout << RAND_MAX << std::endl;`
See what that prints.
• 06-04-2007
MacGyver
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.
• 06-04-2007
robwhit
what is _T()?
• 06-04-2007
Dave_Sinkula
Match parens.
Code:

`_tcscpy(ProductID, _T("{%8X-%4X-%4X-%4X-%12X}", rand(), rand(), rand(), rand(), rand()));`
Are you missing some intervening call in haste?
• 06-04-2007
Daved
>> 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.
• 06-05-2007
disruptor108
Quote:

Originally Posted by swoopy
Code:

`std::cout << RAND_MAX << std::endl;`
See what that prints.

7fff
• 06-05-2007
disruptor108
Quote:

Originally Posted by MacGyver
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?
• 06-05-2007
CornedBee
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.
• 06-05-2007
vart
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last