![]() The "and continuing from there" when he first described it was a bit too vague for me. I'm still not sure I understand just how it works. However for a high-performance solution, I'd definitely go with something else.Īs for George's revised algorithm - um, maybe. Given that it was pretty easy to understand, I wouldn't completely rule it out of consideration. So I tested it a bit to see, and found it wasn't nearly as slow as I might have guessed. However I was intrigued because it is pretty simple to understand, and I was wondering just how fast it actually was. George's original algorithm is not O(n) certainly - I'm not sure what it is offhand, but it's notably worse than O(n). There are various ways to implement this, depending on how precise we want to be with the probabilities - but O(n) is definitely possible. I'd probably prefer to choose the string length first (probability for each length given in Paul Clapham's formula) and then randomly generate a string of that spsecific length. Reid - yes, it's definitely possible to do this in O(n). You will have to compute the number of combinations based on the number of characters You don't have to limit yourself to 26 characters. If you want random strings of max length 3, you use similar logic except that you generate a random number between 0-18271, because the number of combinations are 18272. ![]() So, you select a random number from 0-701, and use number to figure out the string.įor example, if your random number generator gave you 523, you follow these stepsī) character 3 is d, so, next character is d, random string = dĮ) 20 character is t, so next character is t, random string = td String 26 - 701 would be 2 character strings aa,ab,ac,ad.zz. Strings 0-25 would be single character string a,b,c,d.z. You can choose a random number and compute the string based on that number.įor example, if you had to generate a random lower case string of max length 2, then there are 702 possible strings. However, the memory and computational costs of this are ridiculous.Īctually, you don't have to compute all the possible strings. That is: compute all the possible strings, then choose one at random. Finally, return the generated random string.I can think of a most-bogus solution, along the lines of Bogosort.Repeat steps 3 to 5 until the given length using a loop.Concatenate the character into a random string variable.Pick a character from allAlpha at the index position using the charAt() method.Find a random integer (index) using the random() and floor() methods.Declare a string to hold the random string.eg.- “ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz“. Take all alphabetic characters (total 52 with upper/lower case) in a string. ![]() The rounded value will be equal to an integer number.Īpproach to generate random alphabetic String of specific length: ![]() The random() is a static method of Math class generating a random double value between 0.0 to 1.0, and the floor() is also a static method, that rounds the specified double value downward and returns it. In this example, we are going to generate a random alphabetic string of a given size in Java using Math.random() method and Math.floor() method. Generate random alphabetic String of a given length in Java
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |