Global carbon emissions from fossil fuels are projected to decrease by 0.4% in 2026, promising study reveals
Batteries stored in cupboard spark Essex home fire as crews issue warning
Batteries stored in cupboard spark Essex home fire as crews issue warning
What REALLY triggered HSTikkyTokky's boxing night riot between Tommy Robinson and Mo Deen, as Muslim influencer breaks cover to claim 'set up'
Pupils WILL have to hand in phones at the start of the day
Tommy Walsh's final TV appearance on Homes Under The Hammer airs just days before Ground Force star died aged 69 'after a long illness'
Prince William expresses his frustration at how women in the military 'don't feel safe, respected or valued'
Moment Russell Brand flashes card saying 'obey' to Katy Perry on Oscars red carpet after she disagrees with him - as she prepares to 'reveal secrets about her "dark marriage" to convicted rapist'
Russell Brand's 10-year prison sentence for rape is 'lower than the average', expert claims
Maya Jama sips on an espresso martini as star enjoys first night out after revealing seven figure Netflix deal following her departure from Love Island
What are Britons buying on Whatnot? Auction site reveals top items from darts trading cards to old M&S clothes
Made In Chelsea star Tabitha Willett reveals she's had her Hermes Birkin with 'enormous value' stolen from her baby's pram while he was sleeping
Boston Dynamics Adds Wheels to Atlas Humanoid
Read more of this story at Slashdot.
Basildon primary school students are 'not performing well enough', Ofsted says
Hundreds gather in bid to push over Chicago's famous bean sculpture after claims man is trapped inside
The travellers vs the Earl: Traveller who turned idyllic beauty spot into illegal 'prison camp' must tear it down after losing three-year battle with angry villagers
The Morning Poll: Should Britain follow Norway into the EU single market?
CodeSOD: Plenty of IDs
Venkat joined a new team in the middle of a big refactoring effort. The code was quite a mess, glued together by long Python scripts with main functions longer than a Tolstoy novel. It was this effort to clean up the code into neat modules that highlighted some logical issues with their implementation.
def generate_random_number(): """ Generate a 5 digit random number. """ return "".join(map(str, random.sample(range(0,10),5)))The script this function comes from is a launcher script that provisions a VM to execute code. The VM needs a unique identifier, and we get that random number this way.
The random.sample function, in Python, samples a population without replacement. So here, we pass it a range of 0-10 (which is 0-9 inclusive), and ask for five random entries from it. Again, without replacement, so a range that could concievably contain up to 100,000 possible values is significantly constrained down to 30,000 or so- only the numbers less than 100,000 that contain no duplicate digits.
We then map the resulting array into an array of strings, then join that into a final output string.
Now, for most of us, I do suspect that 30,240 unique values is probably enough for how ever many VMs we might need to reserve at any one time; if you're not working for Amazon or Google or whatever, you're probably fine. Then again, the chances of an accidental collision are surprisingly high: after only 26 requests, there's a 1% chance of a collision. And thanks to the birthday problem, it takes 206 requests to get to a 50% chance of collision. So maybe 30,240 is not enough for anybody.
[Advertisement] Plan Your .NET 9 Migration with ConfidenceYour journey to .NET 9 is more than just one decision.Avoid migration migraines with the advice in this free guide. Download Free Guide Now!