- 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?
Edit Admin
Question: Doesn't have Java int64 literals like 100000000L ?
Edit 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.
Edit 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.
Edit Admin
On a hunch: a lot of copy-pasted instances of this function with a couple of sumbols difference.
Edit 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.
Edit 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.
Edit 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.
Edit 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.Edit 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?
Edit Admin
If you are using random numbers in your test, how is that repeatable?