# Thread: Creating Random hex numbers

1. ## 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. 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. Why do I get a 8 digit hex value when the maximum value is only 0x7FFF?

4. In all honesty, I don't think you should if you're using the rand() from stdlib.h/cstdlib.

5. Just out of curiosity, what does this print?
Code:
`_tprintf("%d\n", RAND_MAX);`

6. Code:
```_tprintf(_T("%d",RAND_MAX));

Output: 2020380200```

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

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

8. 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. what is _T()?

10. Match parens.
Code:
`_tcscpy(ProductID, _T("{%8X-%4X-%4X-%4X-%12X}", rand(), rand(), rand(), rand(), rand()));`
Are you missing some intervening call in haste?

11. >> 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. Originally Posted by swoopy
Code:
`std::cout << RAND_MAX << std::endl;`
See what that prints.
7fff

13. 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?

14. 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.

15. 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