- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
At least they were unique Ids, for a given session.
Admin
Isn't the bigger WTF it is TypeScript apparently and is doing implicit string conversion?
Admin
We need to demand truth in advertising. (And function names)
Admin
Unless the task involves monte carlo simulation, or creating "random" one-use passwords, I've always been sceptical of the need to generate random numbers. As it is not a straightforward task to generate truly random numbers, it's often worth taking a step back to find out what the need is for the randomicity.
Admin
commentid5
Admin
What about AI? To simulate a human thinking process, an element of randomness needs to be in the mix.
Admin
The title made me think of ...
Admin
Except that this looks like JavaScript (assuming it's TypeScript), and this code breaks when
counter
reachesNumber.MAX_SAFE_INTEGER
(or2^53
) because2^53 + 1 === 2^53
.Admin
That downstream code should just lower its expectations.
Admin
All numbers are in double-precision floating point in this language?
Admin
It all depends on what context this code is running in. I use code like this in my unit tests. When I'm populating an object to be used as fake data (for example, when I'm faking out a call to an API), the actual contents of the object don't really matter. In that case, I use code like the above to generate a unique number to fill in values:
obj.orderNumber = testHelper.random(); obj.customerNumber = testHelper.random();
So that for a particular unit test, the objects being used in that test have unique values different than any in any other test.
Admin
[Insert Dilbert strip with "9... 9... 9..." random number generator here]
Admin
By coincidence, this whole text is my password for thedailywtf.com
Admin
xkcd 1210
Admin
When using the exponent operation, pretty much. The exponent function is defined using exp(x) and log(x), which are floating-point functions. The lower operators are defined for integers, but for exponents, integers are converted to floating-point, use the exp and log floating-point functions, then only converted back to integer if the next operation requires it. The loop through multiplying integers is not defined for this language.
Math proof available on request.
Admin
I've written code to generate random-enough IDs for items. The reason was that if the IDs are subsequent, people get them mixed up, but if they're random enough that items produced in the same day have different-looking codes, they don't get confused nearly as often. (And yes, we really did have that problem with earlier versions of that software; and yes, this change really did ameliorate, though not eradicate, the problem.)
Admin
@RLB: Me too.
If serial number-like data has to be rekeyed and each unit of work gets a sequential number, many common typos are still valid serial numbers. Just not the correct serial number. Check digits are one common mitigation, but issuing non-sequential (more properly non-local) IDs is another mitigation. And often the latter technique has a smaller impact on downstream code & data plus better statistical performance. IOW, better all around. At a small, but completely local, increase in code complexity.
At least until you discover some fundamental step waaay downstream in the processing pipeline that not only assumes sequential IDs, but demands them. Then you're hosed. Been there, done that too. Ouch!
Admin
My team wrote code almost exactly like this, to replace Kendo's random-GUID function. That random-GUID code was called ~10,000 times per page load, and switching to an incremental counter greatly improved page performance.
(We later ended up replacing Kendo with in-house options, to cut down on load time. I'm not completely certain it was necessary, but we're definitely downloading less code now.)
Admin
I rather suspect this was debug code that didn't get removed.
Admin
I remember reading somewhere about some cryptography function that was returning an hexadecimal string in the form of a few proper English words.
That function was perfectly reversible too. The idea being that a human could remember and enter some words better than they could remember a string of 30 or 50 hexadecimal numbers. But internally, it would mean the same.
Admin
Nope.
A random number generator will give you random numbers. Not unique numbers. Some day, randomly, your tests will fail if they depend on the uniqueness of these numbers. Depending on the range of the random numbers and freuency of tests, it may or may not happen while you are on the same job.
If you want unique numbers, use a unique number generator. Such as presented in the main article here. Make sure to change the name though.
Let us say it all together and remember it for life: Random numbers are not unique numbers.
Addendum 2019-08-20 22:37: On a second reading of your comment, I realized that you are exactly doing the same thing that the coder of the main post did: generating serial numbers by incrementing and calling them random. Now I don't know which is worse.
Admin
I would like to assume that the original code did something random. Then they realised they wanted uniqueness, and that randomness wasn't important, so they changed how it worked without changing the name of the function. Someone else used the same function, expecting it to do what it says on the label, and were bitterly disappointed when they discovered they were lied to.
Admin
Personally, when I want a bunch of IDs that are both unique and non-predictable, I generate them by concatenating a counter and a random part (e.g "3654-42", "8574-43", etc;) (plus a location, if the counter isn't global) As long as you don't do something stupid downstream like comparing both parts separately, it will work well enough.
Admin
https://www.random.org/
Admin
What 3 Words uses a similar concept for geolocation: https://what3words.com/
Admin
I submitted a good WTF article ages ago, but flavorless crud like this continues to get posted instead. Sigh.
Admin
Ugh, that's stupid. If the number is large enough then the chance of collision is negligibly small. There is nothing wrong with random GUID's. Actually, in operation they may be more likely to be unique than a counter, as the counter may be reset for some reason or other, and you need tricks to make the counter work for multiple threads / processes. Note that I am a cryptographer.
Admin
Hmmm. Let us see.
Senseless. What "number"? One can write a "random number generator" which returns the same number which is "large enough" again and again. Being large alone does not decide the chance of collision.
Do you know what the U in GUID stands for? That U is the key to the design of GUIDs, not R, though some implementations may have an element of randomness in them. In fact quoting from wikipedia on version 4 of UUIDs in the "Universally unique identifier" page, In practice, collisions are reported. Such incidents are considered software bugs. Negligibly small?
I never said pure counters are the best solution for every need of uniqueness. Neither did I say one should use bounded integers if counters are used. All I said was random numbers are not unique. And if simple tricks ensure absolute uniqueness (of counters or otherwise generated ids) across threads, processes, or even persistently, why not implement them (or better when possible, use a well known solution implementing them)?
Duly noted.
Admin
Assuming one ID generated per microsecond, you're looking at keeping that javascript session open for 285 years.
I don't even want to imagine what keeping a browser open that long does to your swap.
Admin
@Ulysses Why don't you post your articles in the sidebar at what.thedailywtf.com? Let's have more fun there!
Admin
This is why I love the "Refactor" features in modern IDEs. It removes the laziness barrier for renaming things properly.