This is a cool idea! Here are some thoughts I had:
What are these magic numbers?
You have 4 magic numbers in your main loop: 0x10000
, 0xc369
, 0xFFFF
, and 2
. What do they mean? It looks like 0x10000
is the number of 2-byte words you're writing to the file. It's also the limit of a 16-bit number. It would be nice if there were a named constant for that. Perhaps something like:
const int kWordsPerFile = 0x10000;
or
const int kMax16BitValue = 0x10000;
Or something along those lines. (Technically it's 1 more than the max, so maybe something more descriptive.)
Honestly, I can live with 0xFFFF
but it wouldn't hurt to give it a name like kLSWMask
(where LSW
is least significant word) or something similar.
You could get rid of the 2
by making t
be a uint16_t
and then using sizeof(t)
.
That leaves 0xc369
. I have no idea what it is. I assume this is some sort of linear congruential pseudo-random number generator, but I'm not really well versed in such things. How is it derived? What significance does it have? Give it a name so it's understandable to someone else 6 months from now.
Comments
I'm a big fan of self-documenting code. However, in this case, it would be nice to have at least a sentence explaining what the loop does. Coming across this code in the code base I work on would leave me scratching my head. I'd have to go back through source control comments to see if there was any clue of what it was about. Maybe just a comment like:
// Generate some pseudo-random numbers where no 2 bytes are repeated
would really help a lot! And if you have a link or a short sentence to explain the algorithm, that would be nice, too.
- 11.9k
- 1
- 20
- 46