probability

Random number with Probabilities

别说谁变了你拦得住时间么 提交于 2019-11-26 20:16:24
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. If it is within [51;100] --> I got my random number 3. What would you say? Yours is a pretty good way

Normalizing a list of numbers in Python

谁说我不能喝 提交于 2019-11-26 19:50:41
问题 I need to normalize a list of values to fit in a probability distribution, i.e. between 0.0 and 1.0. I understand how to normalize, but was curious if Python had a function to automate this. I'd like to go from: raw = [0.07, 0.14, 0.07] to normed = [0.25, 0.50, 0.25] 回答1: Use : norm = [float(i)/sum(raw) for i in raw] to normalize against the sum to ensure that the sum is always 1.0 (or as close to as possible). use norm = [float(i)/max(raw) for i in raw] to normalize against the maximum 回答2:

How do I assess the hash collision probability?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 18:42:19
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 time the search system wants to index the same file it will likely give it a different temporary name. The

Fastest primality test

人盡茶涼 提交于 2019-11-26 18:31:10
问题 Could you suggest a fast, deterministic method that is usable in practice, for testing if a large number is prime or not? Also, I would like to know how to use non-deterministic primality tests correctly. For example, if I'm using such a method, I can be sure that a number is not prime if the output is "no", but what about the other case, when the output is "probably"? Do I have to test for primality manually in this case? Thanks in advance. 回答1: The only deterministic, polynomial-time

Random boolean with weight or bias

回眸只為那壹抹淺笑 提交于 2019-11-26 17:49:52
问题 I need to generate some random booleans. However I need to be able to specify the probability of returning true . As a results doing: private Random random = new Random(); random.nextBoolean(); will not work. One possible solution would be: private Random random = new Random() public boolean getRandomBoolean(float p){ return random.nextFloat() < p; } I was wondering if there is a better or more natural way of doing this. EDIT: I guess I am asking whether there is a library class that provides

Nth Combination

放肆的年华 提交于 2019-11-26 17:43:50
Is there a direct way of getting the Nth combination of an ordered set of all combinations of nCr? Example: I have four elements: [6, 4, 2, 1]. All the possible combinations by taking three at a time would be: [[6, 4, 2], [6, 4, 1], [6, 2, 1], [4, 2, 1]]. Is there an algorithm that would give me e.g. the 3rd answer, [6, 2, 1], in the ordered result set, without enumerating all the previous answers? Note you can generate the sequence by recursively generating all combinations with the first element, then all combinations without. In both recursive cases, you drop the first element to get all

Cosmic Rays: what is the probability they will affect a program?

我是研究僧i 提交于 2019-11-26 16:45:58
Once again I was in a design review, and encountered the claim that the probability of a particular scenario was "less than the risk of cosmic rays" affecting the program, and it occurred to me that I didn't have the faintest idea what that probability is. "Since 2 -128 is 1 out of 340282366920938463463374607431768211456, I think we're justified in taking our chances here, even if these computations are off by a factor of a few billion... We're way more at risk for cosmic rays to screw us up, I believe." Is this programmer correct? What is the probability of a cosmic ray hitting a computer and

What are probabilistic data structures?

我与影子孤独终老i 提交于 2019-11-26 16:24:40
问题 I have read about data structures like bloom filters and skip lists. What are the common characteristics of probabilistic data structures and what are they used for? 回答1: There are probably a lot of different (and good) answers, but in my humble opinion, the common characteristics of probabilistic data structures is that they provide you with approximate, not precise answer. How many items are here? About 1523425 with probability of 99% Update: Quick search produced link to decent article on

Is it safe to assume a GUID will always be unique?

心不动则不痛 提交于 2019-11-26 15:50:49
I know there is a minute possibility of a clash but if I generated a batch of 1000 GUIDs (for example), would it be safe to assume they're all unique to save testing each one? Bonus question An optimal way to test a GUID for uniqueness? Bloom filter maybe? Antal Spector-Zabusky Yes, you can. Since GUIDs are 128 bits long, there is admittedly a minute possibility of a clash—but the word "minute" is nowhere near strong enough. There are so many GUIDs that if you generate several trillion of them randomly, you're still more likely to get hit by a meteorite than to have even one collision (from

How to pick an item by its probability?

强颜欢笑 提交于 2019-11-26 15:44:59
I have a list of items. Each of these items has its own probability. Can anyone suggest an algorithm to pick an item based on its probability? Usman Ismail So with each item store a number that marks its relative probability, for example if you have 3 items one should be twice as likely to be selected as either of the other two then your list will have: [{A,1},{B,1},{C,2}] Then sum the numbers of the list (i.e. 4 in our case). Now generate a random number and choose that index. int index = rand.nextInt(4); return the number such that the index is in the correct range. Java code: class Item {