Feed aggregator
CodeSOD: Losing a Digit
Alicia recently moved to a new country and took a job with a small company willing to pay well and help with relocation costs. Overall, the code base was pretty solid. Despite the overall strong code base, one recurring complaint was that running the test suite was painfully long.
While Alicia doesn't specify what the core business is, but says: "in this company's core business, random numbers were the base of everything."
As such, they did take generating random numbers fairly seriously, and mostly used strong tools for doing that. However, whoever wrote their test suite was maybe a bit less concerned, and wrote this function:
public static Long generateRandomNumberOf(int length) { while (true) { long numb = (long)(Math.random() * 100000000 * 1000000); // had to use this as int's are to small for a 13 digit number. if (String.valueOf(numb).length() == length) return numb; } }They want many digits of random number. So they generate a random floating point, and then multiply it a few times to get a large number. If the length of the resulting number, in characters, is the desired length, we return it. Otherwise, we try again.
The joy here, of course, is that this function is never guaranteed to exit. In fact, if you request more than 15 digits, it definitely won't exit. In practice, most of the time, the function is able to hit the target length in a relative handful of iterations, but there's no guarantee for that.
Alicia was tracking down a bug in a test which called this function. So she went ahead and fixed this function so that it use a sane way to generate the appropriate amount of entropy that actually guaranteed a result. She included that change in her pull request, nobody had any comments, and it got merged in.
The unit tests aren't vastly faster than they were, but they are faster. Who knows what other surprises the test suite has in store?
[Advertisement] Keep all your packages and Docker containers in one place, scan for vulnerabilities, and control who can access different feeds. ProGet installs in minutes and has a powerful free version with a lot of great features that you can upgrade when ready.Learn more.Saudi Arabia's Dystopian Futuristic City Project Is Crashing and Burning
Read more of this story at Slashdot.
A Jailed Hacking Kingpin Reveals All About Cybercrime Gang
Read more of this story at Slashdot.
EU Eyes Banning Huawei, ZTE Corp From Mobile Networks of Member Countries
Read more of this story at Slashdot.
The Linux Kernel Looks To 'Bite the Bullet' In Enabling Microsoft C Extensions
Read more of this story at Slashdot.
Critics Call Proposed Changes To Landmark EU Privacy Law 'Death By a Thousand Cuts'
Read more of this story at Slashdot.
PDF Will Support JPEG XL Format As 'Preferred Solution'
Read more of this story at Slashdot.
Meta Is Killing Off the External Facebook Like Button
Read more of this story at Slashdot.
New Project Brings Strong Linux Compatibility To More Classic Windows Games
Read more of this story at Slashdot.
Wikipedia Urges AI Companies To Use Its Paid API, and Stop Scraping
Read more of this story at Slashdot.
The PHP Foundation Is Seeking a New Executive Director
Read more of this story at Slashdot.
World's First Green Fuel Levy To Add Almost $32 To Air Fares
Read more of this story at Slashdot.
Apple Delays Release of Next iPhone Air Amid Weak Sales
Read more of this story at Slashdot.
How HR Took Over the World
Read more of this story at Slashdot.
Africa Finally Has Its Own Drug-Regulation Agency
Read more of this story at Slashdot.
The Algorithm Failed Music
Read more of this story at Slashdot.
Data Centers in Nvidia's Hometown Stand Empty Awaiting Power
Read more of this story at Slashdot.
Tim Berners-Lee Says AI Will Not Destroy the Web
Read more of this story at Slashdot.
Subsea Cable Investment Set To Double As Tech Giants Accelerate AI Buildout
Read more of this story at Slashdot.
Microsoft Bets on Influencers To Close the Gap With ChatGPT
Read more of this story at Slashdot.
