Otherwise, sampspace becomes sampspace - range and range becomes range / chars. In other words, for the values 6 through 13 inclusive, 8 of the 14 possibilities. So, from that code, the first iteration of the final loop will exit with sz = 3 if val is greater than or equal to sampspace - range. If you can limit the maximum size so that your rnd function will work in the range, something like this should be workable: def getlen(chars,maxlen): This solution isn't perfect since you're calling rnd multiple times and it would be better to call it once with an possible range of 26 N+26 N-1+26 1 and select the length based on where your returned number falls within there but it may be difficult to find a random number generator that'll work on numbers that large (10 characters gives you a possible range of 26 10+.+26 1 which, unless I've done the math wrong, is 146,813,779,479,510). In other words, the ratio is 1:26 so 1-character has a probability of 1/27 (rather than 1/26 as I first answered). I use 27 in the above code since the total sample space for selecting strings from "ab" is the 26 1-character possibilities and the 26 2 2-character possibilities. You can use that fact to select your length: def getlen(maxlen): That means there are 26 times as many possibilities for an (N)-character string than there are for an (N-1)-character string. While loop in the ' saveSystemCode' function is preventing to save same value again.Okay, there are 26 possibilities for a 1-character string, 26 2 for a 2-character string, and so on up to 26 26 possibilities for a 26-character string. Super(ClassThree, self).save(*args, **kwargs) Self.systemCode = saveSystemCode(ClassThree, self.systemCode, self.pk, 'three_') Super(ClassTwo, self).save(*args, **kwargs) Self.systemCode = saveSystemCode(ClassTwo, self.systemCode, self.pk, 'two_') Self.systemCode = saveSystemCode(ClassOne, self.systemCode, self.pk, 'one_') While (systemCode=systemCode).exclude(pk=inPK).exists(): So, this is how to generate unique value for all models using saveSystemCode() function : import uuidĭef saveSystemCode(inClass, inCode, inPK, prefix): So I am using a function to generate value. Super(ClassOne, self).save(*args, **kwargs)īut I have same systemCode field in all my Models. While (systemCode=systemCode).exclude(pk=self.pk).exists(): I am using save() method to generate and check this systemCode is unique : def save(self, *args, **kwargs): SystemCode = models.CharField(max_length=25, blank=True, null=True, unique=True) This is my standard class Model : class ClassOne(models.Model): And I am generating this manually, but also sometimes it can take value from user input, so I have to check this value before saving and if it matches, regenerating this value as a unique value.Īnd this is how I generate unique strings at this scenario : I have a unique field, named ' systemCode' within a lot of my models. Here are the results from generating 0-5, they'll always generate the same sequence. We can use a large prime and modulo to make a psuedo-random number like this.Ĭharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789' However since uniqueness is usually more important I'd recommend generating a unique code for each of the numbers from 0 to 36^5 - 1 like this. Since you are going for uniqueness then you have a problem since 36 * 36 * 36 * 36 * 36 = 60'466'176 which will definitely result in collisions if you have millions. If you can afford to lose '8' and '9' in the generated numbers there is a very pythonic solution to getting a random number.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |