When I run  import numpy as np  # Create small random numbers a = np.random.rand(10)  # Check whether their sum and their flipped sum is the same print(np.sum(a) == np.sum(a)) print(np.sum(np.flipud(a)) == np.sum(a))   the first statement is always true, whereas the second statement sometimes returns false, depending on the random numbers, and I have no idea why. I see this in both, Python 2.7 and Python 3.6. Interestingly, if I apply flipud twice like so:  print(np.sum(np.flipud(np.flipud(a))) == np.sum(a))   I always get True.  Edit: Some useful info on a related issue .
9476
Imagine you have a large array of floating point numbers, of all kinds of sizes. What is the most correct way to calculate the sum, with the least error? For example, when the array looks like this:  [1.0, 1e-10, 1e-10, ... 1e-10.0]   and you add up from left to right with a simple loop, like  sum = 0 numbers.each do |val|     sum += val end   whenever you add up the smaller numbers might fall below the precision threshold so the error gets bigger and bigger. As far as I know the best way is to sort the array and start adding up numbers from lowest to highest, but I am wondering if there is an even better way (faster, more precise)?  EDIT: Thanks for the answer, I now have a working code that perfectly sums up double values in Java. It is a straight port from the Python post of the winning answer. The solution passes all of my unit tests. (A longer but optimized version of this is available here )  /**  * Adds up numbers in an array with perfect precision, and in O(n).  *   * @see http://code.activestate.com/recipes/393090/  */ public class Summarizer {      /**      * Perfectly sums up numbers, without rounding errors (if at all possible).      *       * @param values      *            The values to sum up.      * @return The sum.      */     public static double msum(double... values) {         List&lt;Double&gt; partials = new ArrayList&lt;Double&gt;();         for (double x : values) {             int i = 0;             for (double y : partials) {                 if (Math.abs(x) &lt; Math.abs(y)) {                     double tmp = x;                     x = y;                     y = tmp;                 }                 double hi = x + y;                 double lo = y - (hi - x);                 if (lo != 0.0) {                     partials.set(i, lo);                     ++i;                 }                 x = hi;             }             if (i &lt; partials.size()) {                 partials.set(i, x);                 partials.subList(i + 1, partials.size()).clear();             } else {                 partials.add(x);             }         }         return sum(partials);     }      /**      * Sums up the rest of the partial numbers which cannot be summed up without      * loss of precision.      */     public static double sum(Collection&lt;Double&gt; values) {         double s = 0.0;         for (Double d : values) {             s += d;         }         return s;     } }
69175
I roll a fair die and sequentially sum the numbers the die shows. What are the odds the summation will hit exactly 100?  More generally, what are the odds of hitting an exact target number t while summing the results of numbers sequentially drawn uniformly from a set S.  I have experimentally tested this and the result is (warning - spoilers ahead):      1 over the expectation of the drawn numbers. In the case of the fair die 1/((1+2+3+4+5+6)/6) = 6/21 = 2/7   However I do not have a strong intuition, let alone a formal proof, why this is the case.  I'll be happy to get your thoughts!
92088
public class WrapperTest {      public static void main(String[] args) {          Integer i = 100;         Integer j = 100;          if(i == j)             System.out.println("same");         else             System.out.println("not same");     }     }   The above code gives the output of same when run, however if we change the value of i and j to 1000 the output changes to not same. As I'm preparing for SCJP, need to get the concept behind this clear. Can someone explain this behavior.Thanks.
112751
That is to say, the value of a second roll, how much of a bonus value is it worth.  For example: when you make a saving throw, attack or check roll, when does +bonus to the roll and rolling twice equal the same worth.  I believe the value of a second roll would be different depending on if it was 'Choose the Highest', 'Choose the Lowest', 'Choose any Result' and 'Reroll on Failed'.  Though in this case I'm thinking for 'Choose the Highest'.  Edit in response to comments; For the intentions of this question the dice is a d20.
21223
Here is the setup: Bob runs an experiment: he flips a coin N times (between 0 and +$\infty$). The coin has a probability p of landing on heads. Bob starts with zero points. For each head, Bob scores a point. Now, Bob goes to Alice and tells her p and that the score is zero, and ask her the probability that he flipped the coin N=n times. The case that make it special for me is the addition of the possibility of zero draws, and I'm stuck with it. Here is my attempt at solving this: N is the random variable counting the number of flips, S is the random variable counting the score. We want to know $P(N=n|S=0)$. $$ P(N=n|S=0) = \frac{P(S=0|N=n) \cdot P(N=n)}{P(S=0)} $$ Now $P(S=0|N=n)= (1-p)^{n}$ so that's good. Also, $P(S=0) = P(N=0) + \sum\limits_{n=1}^\infty (1-p)^{n}P(N=n) $ But now I'm stuck since I don't know what do with that $P(N=n)$. Now you could argue that since we know nothing about the distribution of N, the most reasonable assumption is that they are all equally likely. Then the substitution occurs as: $$ P(N=n|S=0) = \frac{P(S=0|N=n) \cdot P(N)}{\frac{1}{p}\cdot P(N)} = p(1-p)^{n} $$ This feels like cheating. Not because of the limit, this looks legit cf replace $\infty$ with $N_{max}$ and take the limit at the end, but because of the assumption on $P(N)$ being uniform. While keeping $N_{max}$ this is good, but it seems shady when taking the limit. Also, what do I know if it is uniform ? Am I doing something wrong here ?
36924
Is there a reasonable statistic test one can do to standard random number generators (say, one of those that come built in in Python libs) which shows they are not really random?  (By reasonable I mean to exclude silly things like «generate a looong list and see that it is periodic»)  Later: Notice I am looking for a test that fails on standard generators.
69633
Let's say you tell me to produce randomly a number from 1-100, and I choose the number 47. Can it be said that there is a specific reason I chose the number 47, and that it is not completely random? By random I mean that there is absolutely no reason this number in particular was chosen.    It seems odd to me, since it seems like since I have applied conscious effort to produce or choose a number within that range, or even without range, that it can't be truly random, but there might be a reason why I chose that number amongst the many others I could have chosen.  I guess the general question, which I'm interested to know if any philosophers in the past have discussed, is whether our thoughts are necessarily based on prior thoughts, or if thoughts can be truly spontaneous and random?
25462
Let $\pi_1,...,\pi_n$ be a random permutation of numbers $1,...,n$. If you are told that $\pi_k &gt; \pi_1,...,\pi_k &gt; \pi_{k-1}$, what is the probability that $\pi_k = n$?  What I've tried:  Let $A_j$ be the event that $\pi_k = j$. Let $H$ = be event that $\pi_k &gt; \pi_1,...,\pi_k &gt; \pi_{k-1}$.  Then $P(A_n|H) = \frac{P(H|A_n)P(A_n)}{\sum_{j = 1}^n P(H|A_j)P(A_j)} = \frac{\frac{1}{n}}{\sum_{j=1}^n P(H|A_j)\frac{1}{n}} = \frac{1}{\sum_{j=1}^n P(H|A_j)} = \frac{1}{\sum_{j=1}^n P(j &gt; \pi_1,...,j &gt; \pi_{k-1})}$.  How to calculate that last sum?
34029
Question:     6 unbiased dice are tossed together. What is the probability that the sum of all the dice is an even number?   I think the answer would be 50%, purely by intuition. However, not sure if this is correct. How should I go about solving such a problem?
9038
Randomly break a stick (or a piece of dry spaghetti, etc.) in two places, forming three pieces.  The probability that these three pieces can form a triangle is $\frac14$ (coordinatize the stick form $0$ to $1$, call the breaking points $x$ and $y$, consider the unit square of the coordinate plane, shade the areas that satisfy the triangle inequality edit: see comments on the question, below, for a better explanation of this).  The other day in class*, my professor was demonstrating how to do a Monte Carlo simulation of this problem on a calculator and wrote a program that, for each trial did the following:   Pick a random number $x$ between $0$ and $1$.  This is the first side length. Pick a random number $y$ between $0$ and $1 - x$ (the remaning part of the stick).  This is the second side length. The third side length is $1 - x - y$. Test if the three side lengths satisfy the triangle inequality (in all three permutations).   He ran around $1000$ trials and was getting $0.19$, which he said was probably just random-chance error off $0.25$, but every time the program was run, no matter who's calculator we used, the result was around $0.19$.  What's wrong with the simulation method?  What is the theoretical answer to the problem actually being simulated?  (* the other day was more than $10$ years ago)
95659
We would like to choose between theatre and cinema by tossing a coin. Unfortunately the only available coin we have has probapility of heads $p\ \left(\dfrac{1}{2}&lt;p&lt;1\right)$. How could we use that coin to take a good decision so that the two options have equal possibilities? My solution: we will flip the coin twice, if it comes up heads first and tails second, then we will go to the theatre. If it comes up tails first and heads second, then we will go to the cinema. If the two flips are the same, we flip twice again, and repeat the process until we have a unbiased toss. Is it correct?    Note added by joriki: As discussed in the comments, I'm reopening the question because of the aspect that the probability $p$ of heads is known and may be used to optimise the algorithm; this aspect was not present in the question  as a duplicate of which this question had been closed.
22632
Suppose I have 3 dice. Each has some mechanism that can prevent other dice being in the same number as itself when they are rolled together. Now I roll the 3 dice at the same time. Then what is the probability of having a 2 appear?  Is the answer 1/6 + 1/6 + 1/6 = 1/2 since the event are mutually exclusive and each has a probability of 1/6? But 1/2 seems to be too high?
58873
Does this identity hold for all events?  $$ P(A|B) = 1-P(A'|B) $$  Logically speaking, if the probability of $A$ given $B$ occurred is $X$, shouldn't the probability that $A$ does not occur, $A'$, given $B$, be similarly $1-X$?  There is a related question .  This is the closest that I could get to proving (or disproving) it:   $P(A\cap B)=P(A)-P(A' \cap B)$ $P(A|B)P(B)=P(A)-P(A'|B)P(B)$ $\therefore P(A'|B)= \frac {P(A)} {P(B)}-P(A|B)$   Are there are certain formulae which can be used to prove this? Or does the identity only hold under certain situations, and if so, what kind of situations?  Thanks.
66309
A question in probability by a non-mathematician:  A fair coin is tossed $2N$ times. Is it unlikely that we get exactly $N$ heads and $N$ tails?  From one side, this must be the most likely result! But intuitively, if someone reports to me that they threw 2,000,000 coins and got exactly 1,000,000 heads and 1,000,000 tails I would be a little suspicious.  If this really is suspicious, how does it compare to biased results? Obviously, 2,000,000 heads is a much more suspicious result. But, for what value of $K$ is $N+K$ heads and $N-K$ tails as suspicious as $N$ heads and $N$ tails?  Equations are very welcome, but please try to provide some aid in understanding them.  ADDED: Henning Makholm's answer helped me understand better what I want to ask: I always assume a fair coin and would like to compare the probability of the split N/N with the probability of getting either more than more than N+K or less than N-K heads. By ncmathsadist's answer , the probability for the N/N split goes like 1 over the square root of N.  What about the probability to get more than either more than N+K or less than N-K heads? For what value of K (approximately) does it equal to 1/sqrt(n pi)?  CLARIFICATION: If f(N) is the probability to get an N/N split in 2N tosses of a fair coin, and g(N,K) is the probability to get more that N+K heads or less than N-K heads in 2N tosses of a fair coin, then I am asking for a formula which, given N, approximates the values of K for each f(N) is closest to g(N,K). This is a formula that takes N and returns K so we can call it h(N)=K. Which kind of function is h? My guess from a little experementing with the online software in Brian M. Scott's answer is that the function h is apporximately the SQRT function, multiplied by some constant, or something like that.
17348
I have this question that I don't know where to begin, any help will be greatly appreciated.  Consider the following random experiment. Toss a coin until the same number of heads and tails have been observed. Take as random variable, X, the observed number of heads (or tails). Determine the first few values in the probability distribution and calculate  $\sum^{5}_{n=1} nP (X = n)$  I'm not sure exactly where to start, if someone could point me in the right direction that would be great! thanks
37331
I have a coworker who writes unit tests for objects which fill their fields with random data. His reason is that it gives a wider range of testing, since it will test a lot of different values, whereas a normal test only uses a single static value.  I've given him a number of different reasons against this, the main ones being:   random values means the test isn't truly repeatable (which also means that if the test can randomly fail, it can do so on the build server and break the build) if it's a random value and the test fails, we need to a) fix the object and b) force ourselves to test for that value every time, so we know it works, but since it's random we don't know what the value was   Another coworker added:   If I am testing an exception, random values will not ensure that the test ends up in the expected state random data is used for flushing out a system and load testing, not for unit tests   Can anyone else add additional reasons I can give him to get him to stop doing this?  (Or alternately, is this an acceptable method of writing unit tests, and I and my other coworker are wrong?)
82881
Before reading the Wikipedia article, my idea to calculate the probability was as follows:  $1-\left(\frac{364}{365}\right)^{_nC_{2}}$  Basically, I thought to compare all combinations of pairs ($_nC_{2}$) and require them to have different birthday ($\frac{364}{365}$). It gave a very similar result to the wiki method, but is slightly different and I can't tell how  my way of thinking is wrong.  Can anyone tell me what I'm missing?