![]() I used longs for the population sizes you could probably muck around with using doubles instead for the probabilities. Then R(i+i) will be randomly chosen between R(i) and M(i+1),Īnd the relative probabilities of the choice are determined by M(i) is a random string of exactly length 'i' R(i) is a random string of length at most 'i'. The basic idea is that you can set up a recurrence relationship to solve the problem. ("time = " + (System.currentTimeMillis() - time)) Int intArray = s.getArray(maxLength, numChars) */long time = System.currentTimeMillis() Public int getArray (int maxLength, int numChars) MaxLength is the max string size and numChars is the number of available characters. Or more to the point, can you show code which implements the algorithm you're thinking of? There are several more choices to make in various special cases, and so far I can't see a version which will really give equal probabilities of all strings, but I could well be missing something.Ī quick version of the code that I was discussing in the 'shift' approach: Which of the following contain "embedded zeros" which justify shifting and rerolling, and which are non-embedded zeros that actually reduce the length of the string? However I think the whole thing is still a bit fuzzy. I made a guess what it might mean, which was apparently wrong. ![]() 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. I can think of a most-bogus solution, along the lines of Bogosort.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |