- 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
https://xkcd.com/221/
Admin
Who could have known that some knowledge of maths would be useful for programmers?
Admin
Question: Doesn't have Java int64 literals like 100000000L ?
Admin
Yes, of course, the programmer could just have written 10000000000000000L or something.
Admin
It does, but I wouldn't expect the author of this code to know that.
Admin
Admin
Or more readable: 10_000_000_000_000_000L
Admin
Of course, using the right tools would be too hard...
And if this requires a Java version < 17, then
ThreadLocalRandomcan be used instead ofSecureRandom, that existed with thenextLong(origin, bound)method since Java 7.Admin
I've tested my code snippet above. It can generate up to length 24, and can do so in milliseconds at most.
Admin
@Jaloopa: If you're going to iterate, it's faster to keep it in the numeric domain & skip the string munging. Something close to this net of my (uneditable) mistakes:
Addendum 2025-11-11 08:31: Seems a few people snuck in above me with similar ideas while I was elsewhere. Sorry to pile-on.
Admin
On a hunch: a lot of copy-pasted instances of this function with a couple of sumbols difference.
Admin
I'm not sure about this. If Java is similar in it's implementation of randomness with .net then the cryptographic random generators are considerably slower than the regular ones. So using one when there is no business requirement for cryptographic safe randomness is a complete waste of resources.
Admin
And after a long numb, this function will (maybe) return numb.
Admin
In a test suite, the XKCD random number generator is not necessarily a bad option in some circumstances.
If you want your tests to be repeatable, your random number generator should definitely be deterministic e.g. use a pseudo random number generator with a known seed. In the past, when I've wanted to exercise specific code paths, I've written "random" number generators that merely return successive numbers from a list that I hand crafted.
Admin
public static Long generateRandomNumberOf(int length) { long result = 0; var random = new Random(); for (var i = 0; i < length; i++) { var = var * 10 + random.NextInt(0, 9).ToLong(); } return result }
This returns zero. long result = 0; is set at the top of the method and is never used. I think you meant to cast var as a long and assign it to result?
Admin
If they want the random number to have a given length(and no lower) then you can't just do this becauae there's a 1/10 chance the fiest number is 0(even if you fixed your other bugs).
The best sololution is to simply do something like:
This guarantees the number is at least 10^(len-1) and at most (10^len)-1 while being very easy to read.
Admin
Here is the fastest random generator for a given length:
Addendum 2025-11-11 10:02:
_rndcan be static or not, it's up to your seeding requirements._powersOf10isstatic readonly- this snippet above was made in sharplab.io, I was lazy to format the code as needed.Admin
See my solution, it's better to cache powers of 10, and .NET has built-in
Random.NextInt64these days. If you have to use Java and it doesn't have something likeNextInt64, then your code can be improved to cache the powers, then it's just multiplication and addition.Addendum 2025-11-11 10:09: PS. Never mind, just saw Rob's comment, Java does have
nextLong, so that's the solution, with cached powers of 10.Admin
The real wtf is that Java doesn't have an easily accesible method of generating a random number of exactly length n. I'm sure there are libs that solve this, but shouldn't it be built in?
Admin
The real wtf is that Java doesn't have an easily accesible method of generating a random number of exactly length n. I'm sure there are libs that solve this, but shouldn't it be built in?
Admin
If you are using random numbers in your test, how is that repeatable?
Admin
Generating random numbers of specific decimal length isn't a common scenario compared to generating a random number between a specific lower and upper bound, and generating a specific number of random bytes. Both of these Java does have.
Admin
Generating a random integer of length n is equivalent to generating a random integer between 10^(n-1) and 10^n-1. E.g. a 3-digit number is from 100 to 999.
Admin
How is it repeatable? Quite easily. You can use the same random number generator and use the same seed.
Admin
I too work in an industry where random number generation is taken very seriously (to the point where I suspect it's the same one as the OP), and I too would be horrified to come across that code. But I used to test other companies' implementations of RNGs for compliance, and saw a lot of nonsense like this everywhere. If your first thought is ever to use strings to build your random number, just stop. That's not how computers work, that's not how RNGs work, that's just how humans like to look at things. Numbers should be turned into strings for display purposes only.
Admin
So the first digit is less random than the others because it’s guaranteed to never be zero? That seems … problematic?
Admin
Well, to be fair, this is .net code. So even if you port Java code 1:1 it's on average twice as fast on .net simply because of the different technological stack at play.
Admin
I am who sent this code and I'd like to give a bit more context: This was a long time ago and I no longer work there; I am not even in the country anymore, so some details could be a bit foggy in my memory. I see some are already on the right track (as in, how I fixed it). This wasn't really an issue at the time; I just HAD to fix it. The code had been there since the last source control migration, i.e. for ever and nobody seemed to know who wrote it. Nobody seemed to be bothered with me changing it either. There's probably a better way to fix it, but I didn't want to spend much time in this specific function; I was the "new one" and had a bug fix to deliver.
This was my fix: