probability

Data structure for loaded dice?

烂漫一生 提交于 2019-11-26 15:36:57
Suppose that I have an n-sided loaded die where each side k has some probability p k of coming up when I roll it. I'm curious if there is good algorithm for storing this information statically (i.e. for a fixed set of probabilities) so that I can efficiently simulate a random roll of the die. Currently, I have an O(lg n) solution for this problem. The idea is to store a table of the cumulative probability of the first k sides for all k, them to generate a random real number in the range [0, 1) and perform a binary search over the table to get the largest index whose cumulative value is no

Creating your own Tinyurl style uid

两盒软妹~` 提交于 2019-11-26 15:29:24
问题 I'm writing a small article on humanly readable alternatives to Guids/UIDs, for example those used on TinyURL for the url hashes (which are often printed in magazines, so need to be short). The simple uid I'm generating is - 6 characters: either a lowercase letter (a-z) or 0-9. "According to my calculations captain", that's 6 mutually exclusive events, although calculating the probability of a clash gets a little harder than P(A or B) = P(A) + P(B), as obviously it includes numbers and from

Choosing n numbers with fixed sum

我们两清 提交于 2019-11-26 14:38:19
In some code I want to choose n random numbers in [0,1) which sum to 1 . I do so by choosing the numbers independently in [0,1) and normalizing them by dividing each one by the total sum: numbers = [random() for i in range(n)] numbers = [n/sum(numbers) for n in numbers] My "problem" is, that the distribution I get out is quite skew. Choosing a million numbers not a single one gets over 1/2 . By some effort I've calculated the pdf, and it's not nice. Here is the weird looking pdf I get for 5 variables: Do you have an idea for a nice algorithm to choose the numbers, that result in a more uniform

Generate random number with given probability matlab

穿精又带淫゛_ 提交于 2019-11-26 11:21:56
I want to generate a random number with a given probability but I'm not sure how to: I need a number between 1 and 3 num = ceil(rand*3); but I need different values to have different probabilities of generating eg. 0.5 chance of 1 0.1 chance of 2 0.4 chance of 3 I'm sure this is straightforward but I can't think of how to do it. The simple solution is to generate a number with a uniform distribution (using rand ), and manipulate it a bit: r = rand; prob = [0.5, 0.1, 0.4]; x = sum(r >= cumsum([0, prob])); or in a one-liner: x = sum(rand >= cumsum([0, 0.5, 0.1, 0.4])); Explanation Here r is a

Why is XOR the default way to combine hashes?

感情迁移 提交于 2019-11-26 11:04:01
Say you have two hashes H(A) and H(B) and you want to combine them. I've read that a good way to combine two hashes is to XOR them, e.g. XOR( H(A), H(B) ) . The best explanation I've found is touched briefly here on these hash function guidelines : XORing two numbers with roughly random distribution results in another number still with roughly random distribution*, but which now depends on the two values. ... * At each bit of the two numbers to combine, a 0 is output if the two bits are equal, else a 1. In other words, in 50% of the combinations, a 1 will be output. So if the two input bits

Generate random integers with probabilities

狂风中的少年 提交于 2019-11-26 10:51:06
问题 I\'m a bit confused about how to generate integer values with probabilities. As an example, I have four integers with their probability values: 1|0.4, 2|0.3, 3|0.2, 4|0.1 How can I generate these four numbers taking into account their probabilities? 回答1: Here's a useful trick :-) function randomWithProbability() { var notRandomNumbers = [1, 1, 1, 1, 2, 2, 2, 3, 3, 4]; var idx = Math.floor(Math.random() * notRandomNumbers.length); return notRandomNumbers[idx]; } 回答2: A simple naive approach

JavaScript - How to randomly sample items without replacement?

拈花ヽ惹草 提交于 2019-11-26 09:58:43
问题 JavaScript I\'ve tried searching for something like this, but I am not able to find it. It\'s a simple idea: a. Take a random number between 0 to 10. b. Let\'s say the random number rolled is a 3. c. Then, save the number (the 3). d. Now, take another random number again between 0 to 10, but it can\'t be the 3, because it has already appeared. 回答1: One solution is to generate an array (a "bucket") with all the values you want to pick, in this case all numbers from 0 to 10. Then you pick one

Probability of collision when using a 32 bit hash

China☆狼群 提交于 2019-11-26 09:16:41
问题 I have a 10 character string key field in a database. I\'ve used CRC32 to hash this field but I\'m worry about duplicates. Could somebody show me the probability of collision in this situation? p.s. my string field is unique in the database. If the number of string fields is 1 million, what is probability of collision ? 回答1: Duplicate of Expected collisions for perfect 32bit crc The answer referenced this article: http://arstechnica.com/civis/viewtopic.php?f=20&t=149670 Found the image below

Random number with Probabilities

混江龙づ霸主 提交于 2019-11-26 07:29:32
问题 I am wondering what would be the best way (e.g. in Java) to generate random numbers within a particular range where each number has a certain probability to occur or not? e.g. Generate random integers from within [1;3] with the following probabilities: P(1) = 0.2 P(2) = 0.3 P(3) = 0.5 Right now I am considering the approach to generate a random integer within [0;100] and do the following: If it is within [0;20] --> I got my random number 1. If it is within [21;50] --> I got my random number 2

How do I assess the hash collision probability?

拥有回忆 提交于 2019-11-26 06:30:58
问题 I\'m developing a back-end application for a search system. The search system copies files to a temporary directory and gives them random names. Then it passes the temporary files\' names to my application. My application must process each file within a limited period of time, otherwise it is shut down - that\'s a watchdog-like security measure. Processing files is likely to take long so I need to design the application capable of handling this scenario. If my application gets shut down next