Another story on random == unique wtf
-
This gem existed some years ago in our codebase.
The problem that the ingenious programmer tried to solve? Create a handful of artificial namespace prefixes for use in some generated xml output.
private String createname(){ char chars []= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; String str = String.valueOf(chars); String name = ""; for(int i=0; i<3; i++){ name = name + str.charAt((int)((Math.random()*26)%26)); } return (name); }
I once saw a prefix named "bug". "Curious", I thought, and ignored it.
The second time it caught my attention was when it exploded.
The moral: always pay attention to what your code tries to tell you.
-
I think you should just change that 3 to 4 and wait for the really interesting words to show up.
-
Yeah, but why restrain myself to that? I could just change it to 1,145,589 and wait till the Iliad comes out. Epic poetry embedded in XML? Customers would love it!
-
butitcouldusesomespacetheretooandperhapssomepunctuation
-
@DOA said:
I think you should just change that 3 to 4 and wait for the really interesting words to show up.
Words like 'aaaa'? Because the only thing this function will ever generate is a stream of 'a'.
The random value is first multiplied by 26, making it a multiple of 26. Then it is modded by 26, which will always be zero.
-
@smxlong said:
@DOA said:
I think you should just change that 3 to 4 and wait for the really interesting words to show up.
Words like 'aaaa'? Because the only thing this function will ever generate is a stream of 'a'.
The random value is first multiplied by 26, making it a multiple of 26. Then it is modded by 26, which will always be zero.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html
public static double random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
-
@julmu said:
@smxlong said:
@DOA said:
I think you should just change that 3 to 4 and wait for the really interesting words to show up.
Words like 'aaaa'? Because the only thing this function will ever generate is a stream of 'a'.
The random value is first multiplied by 26, making it a multiple of 26. Then it is modded by 26, which will always be zero.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html
public static double random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
I nominate you, sir, for the most brillant person of the thread. Congrats!
-
@ounos said:
I nominate you, sir, for the most brillant person of the thread. Congrats!
I don't get it. Why do you think that I'm brillant?
-
Your browser settings must be hiding the <irony> tags all around. But you can't see that.