2 hours ago I thought I had this figured out, but now I am doubting myself and want someone to validate my algorithm.   I want to take a stream of k trusted random bits and convert it to groups of 5 digits in a uniformly distributed range from 0-99999. To do this I take k and truncate to a multiple of 32, let's say the result now has n bits where n = k - k % (4*8). To create one group of 5 digits we load 32 bits into a variable we'll call var_i. The result will be stored in var_e. To get 5 digits from 0-99999 takes 17 bits, so we perform the loop:      for (int i = 0; i &lt; 17; i++)       var_e += ((var_i &gt;&gt; i) % 2) &lt;&lt; i   this way we preserve only the first 17 bits. Then we perform var_e = var_e % 100000 to truncate var_e to 5 digits. var_e is now one complete block of 5 digits.  An alternative algorithm could be to just take a 32 bit number var_e and perform var_i = var_e % 100000. This would take just the last 5 digits of the 32 bit number, which fall into the range 0-99999.  These algorithms both waste nearly 50% of the input data, but it would be fairly easy to change the first one to accept 17 bits of input.
Say that we have a secure random number generation that outputs 32 bit random numbers, so it's output is a true random number between 0 and a MAX.  What is the best way to map this random number to a defined range (say I want something random between X and Y) without loosing too much entropy?
The question is $$\text{If }\, p, \ q\in \mathbb{N}, \;1-\frac12+\frac13-\frac14-\dotsb-\frac{1}{1318}+\frac{1}{1319}=\frac{p}{q}.\qquad \text{Prove that } 1979\mid p.$$ So my solution went like this:  $$S=1-\frac12+\frac13-\frac14-\dotsb-\frac{1}{1318}+\frac{1}{1319}\Rightarrow S=\sum_{k=1}^{1319}\frac{1}{k}-2\sum_{k=1}^{659}\frac{1}{2k}=\sum_{k=660}^{1319}\frac{1}{k}$$ Now this is equivalent to finding the sum of the roots of the equation $$\left(x-\frac{1}{660}\right)\left(x-\frac{1}{661}\right)\dots \left(x-\frac{1}{1319}\right)=0$$. Rewriting this equation, we get $(660x-1)(661x-1)\dotsc(1319x-1)=0$ (we can discard the $\displaystyle \frac{1319!}{659!}$ since $1979$ is a prime). Calculating the sum of the roots, we get the required value to be equal to $(1320+659)\times 330$ which is obviously divisible by $1979$ (again discarding the coeffecient of $x^{660}$).  Now my question is: Using the same method of converting the problem into finding the sum of roots of a polynomial, we get from the original equation something like this: $$(x-1)\left(x+\frac{1}{2}\right)\dots \left(x-\frac{1}{1319}\right)=0 \Rightarrow (x-1)(2x+1)\dotsc(1319x-1)=0$$ So, the sum of roots would be $-1+2-3+4-\dotsc+1318-1319=-660$ But this is not divisible by $1979$. Why does the former solution work while the latter doesn't? Is there a flaw in my reasoning? Note: I know that the question can be solved by some other methods. Please only explain my given doubts and don't instead give another solution  I realised my mistake after some hours of pondering over it. (In the 1st equation) To get the required sum I would need to take calculate $\displaystyle -\frac{\text{coeffecient of }x^{659}\,}{\text{coeffecient of }x^{660}}$. However what I've done is calculate this:  $\displaystyle -\frac{\text{ coeffecient of }x}{\text{coeffecient of }x^{660}}$ which is actually equal to $\text{sum of roots taken }659 \text{ at a time} $. It was just a coincidence that the value I calculated was divisible by $1979$. The correct way, indeed, would be through the link supplied How to test if these two samples are from the same distribution? (These are tweets located near some tourist attractions.     If yes, is there a simple implementation in Python? Trying to find a story about an unborn telepathic baby who can save a spaceship from doom Why should I use the keyword "final" on a method parameter in Java? I can't understand where the final keyword is really handy when it is used on method parameters.    If we exclude the usage of anonymous classes, readability and intent declaration then it seems almost worthless to me.   Enforcing that some data remains constant is not as strong as it seems.    If the parameter is a primitive then it will have no effect since the parameter is passed to the method as a value and changing it will have no effect outside the scope.  If we are passing a parameter by reference, then the reference itself is a local variable and if the reference is changed from within the method, that would not have any effect from outside of the method scope.    Consider the simple test example below. This test passes although the method changed the value of the reference given to it, it has no effect.  public void testNullify() {     Collection&lt;Integer&gt; c  = new ArrayList&lt;Integer&gt;();           nullify(c);     assertNotNull(c);            final Collection&lt;Integer&gt; c1 = c;     assertTrue(c1.equals(c));     change(c);     assertTrue(c1.equals(c)); }  private void change(Collection&lt;Integer&gt; c) {     c = new ArrayList&lt;Integer&gt;(); }  public void nullify(Collection&lt;?&gt; t) {     t = null; } In Dummit and Foote, Exercise 15.18 (p. 727 3rd edition) one is asked to prove that for a commutative ring with unity $R$, and $f$ a non-nilpotent element of $R$, that the localization $R_f$ is isomorphic to $R[x]/(xf - 1)$.  I have done this, using the strategy outlined in the problem, using the universal property of $R_f$ proved in Theorem 36. The problem I have is this:  If $R_f \cong R[x]/(xf - 1)$ and $\phi:R[x] \to R_f$ is a surjective homomorphism, it stands to reason that $\text{ker}(\phi)$ should be $(xf - 1)$. I cannot show this directly. The surjective homomorphism I have in mind is, for:  $p(x) = a_0 + a_1x + \cdots + a_mx^m$  $\phi(p) = \dfrac{a_0}{1} + \dfrac{a_1}{f} + \cdots + \dfrac{a_m}{f^m}$  It is clear to me that $\phi$ annihilates $(xf - 1)$, so that ideal is contained in the kernel. What I cannot show is the other containment, or equivalently:  If $\phi(g) = 0_{R_f}$ then $g(x) \in (xf - 1)$. The trouble I am running into is that $R$ might be a "bad" ring, containing a lot of zero-divisors, $f$ might even be a zero-divisor (being non-nilpotent is a considerably weaker property), so the usual polynomial tricks of the division algorithm or using the irreducibility of $xf - 1$ aren't much use, here.  I feel I am missing something obvious....what is it? Vieta Jumping: Related to IMO problem 6, 1988: If $ab + 1$ divides $a^2 + b^2$ then $ab + 1$ cannot be a perfect square. The famous IMO problem 6 states that if $a,b$ are positive integers, such that $ab + 1$ divides $a^2 + b^2$, then $\frac{ a^2 + b^2}{ab + 1 }$ is a perfect square, namely, $gcd(a,b)^2$.  How about a modification of this problem:    If $a,b$ are (strictly) positive integers, such that $ab + 1$ divides $a^2 + b^2$, then $ab + 1$ cannot be a perfect square.    I am looking for a proof of the claim above, or a counter-example.  One possible approach towards a proof is the following:   Suppose there is such a pair $(a,b)$ as above, then by the famous IMO problem 6 from 1988, $\frac{ a^2 + b^2}{ab + 1 } = g^2$ where $g = gcd(a,b)$. Since $ab + 1$ is a perfect square, $a^2 + b^2 = c^2$ for some integer $c$. So that $(a,b,c)$ is a Pythagorean tripple, therefore there exists positive integers $n,m,l$ with $n$ coprime to $m$, such that $a = l(n^2 - m^2)$ and $b = 2lnm$ - by Euclids formula.  Then by plugging in $a$ and $b$ in terms of $n,m,l$ into the original equation, and solving for $l$, it is possible to obtain the following:  There exists positive coprime integers $n,m$ such that   $\frac{(n^2 + m^2 + 1)(n^2 + m^2 - 1)}{2mn(n+m)(n-m)}$ is a perfect square.  If we put the quotient above into a program, as in this python code snipet:  N = 1000  for n in range(1,N):     for m in range(n+1, N):         A = (n*n + m*m + 1)*(n*n + m*m - 1)         B = 2*m*n*(n+m)*(m-n)         if A % B == 0:             print(A/B)   The quotient always is 2, regardless of whether or not $n$ and $m$ are co prime! So if the very strong implication   $2mn(n+m)(n-m) \mid (n^2 + m^2 + 1)(n^2 + m^2 - 1) \implies \frac{(n^2 + m^2 + 1)(n^2 + m^2 - 1)}{2mn(n+m)(n-m)} = 2$    holds, then the original problem will be solved. Blank lines in an equation and some other similar function Why blank lines between equation makes error? there are a lot of commands in latex and almost for all of them these blank lines are not important,  but why in this case is important and is there any similar function? We have previously determined that  or  can attune to magic items, but one of those is a sapient creature, and the other "always obeys your commands".  A paladin's steed (per the Find Steed spell) is also 'unusually intelligent' and falls within the score range for sapient creatures.  But what about a regular old animal? If my ranger has a wolf companion, can he make that wolf attune to, say, an Amulet of Health? According to the Beast Master archetype, a companion animal "obeys your commands as best it can".  Is spending an hour 'focused only on the item' (as required for attunement) something a beast can manage?  Or what if it's not an Animal Companion, but just a riding horse, a pet dog, a trained magpie?  I know, probably this is going to be down to the DM to determine, but is there any guidance we can use to help decide whether my cat can attune to an item, and whether rubbing it with catnip would help? Expansion of $(1+\sqrt{2})^n$ I was asked to show that $\forall n\in \mathbb N$ there exist a $p\in \mathbb N^\ast$ such that $$(1+\sqrt{2})^n = \sqrt{p} + \sqrt{p-1}$$   I used induction but it wasn't fruitful, so I tried to use the binomial expansion of $(1+\sqrt{2})^n$ but it seems I lack some insight to go further.   Any hint is welcomed. How can I disable Ctrl+Q for all applications (system wide)? A lot of applications like Firefox, Chrome etc, get closed when I accidentally press Ctrl+Q instead of Ctrl+W because of the proximity of the Q and W keys on the keyboard. Is there a way this shortcut can be removed or reset on a system wide basis? 2 GROUP BY WITH DISTINCT , SUM , COUNT : PHP MYQL Ref :   I got answer (thanks coytech ) but i need one more column in its :  as per :  id | mid | pid | owgh | nwgh | 1    3      12    1.5    0.6 2    3      12    1.5    0.3 3    3      14    0.6    0.4 4    3      15    1.2    1.1 5    4      16    1.5    1.0 6    4      17    2.4    1.2  7    3      19    3.0    1.4   i got answer      Select mid ,          COUNT(distinct pid) as cpid  ,          SUM(nwgh) as totalnwgh  from test  GROUP BY mid   sqlfiddle :   mid  cpid       totalnwgh 3      4          3.8 4      2          2.2    But above i need one more column that's as below : totowgh  mid cpid        totalnwgh  totowgh 3      4          3.8        6.3 (DISTINCT value as per pid column) 4      2          2.2        3.9   where totowgh = 6.3 come by DISTINCT value of owgh column  as per distinct pid column  mid = 3 has count 5 but distinct pid  = 4 for mid=3 same way "distinct" owgh = 6.3 for mid=3 and distinct pid.  As pid=12 , column owgh value 1.5 is twice ...hence its is count 1 time hence,  1.5 + 0.6 + 1.2 + 3 = 6.3 ( please not this is as per DISTINCT value of pid )  Please note : i need distinct owgh value as per distinct pid or group by pid .. because if i replace value of owgh 0.6 with 1.5 then it will be 5.7 instead of 7.2 but value of owgh 0.6 belong to pid = 14 and not pid = 12 hence totalcount of owgh change ...but i need is 7.2  SEE WHAT I MEANS   : Proving that $m+n\sqrt{2}$ is dense in $\mathbb R$ I am having trouble proving the statement:     Let $$S = \{m + n\sqrt 2 : m, n \in\mathbb Z\}$$ Prove that for every $\epsilon &gt; 0$, the intersection of $S$ and $(0, \epsilon)$ is nonempty. Get text between two strings Regex VB.Net Making leaflet control open by default I'm using leaflet to display a map.  I have a control section that I'd like to be open by default, but I can't find a way to do it through the . Is there a way to do this? Are C++ Templates just Macros in disguise? How do I hide javascript code in a webpage? Repairing corrupt MP4 I took a video from my Android phone (Sony Xperia P, if you need to know), and before I could stop recording it, the phone memory got full and the phone got hanged too. I rebooted the phone, the video was there, but I couldn't play it from any media player on Android. It's in mp4 format.    So I tried to open it on PC and Mac with different video player, like VLC, quick Time, Windows Media Player etc., but none of them can play it. I know that the video is corrupted, but I believe it's repairable, since it got more than a GB in size. Is there anything that I can try  to repair the video file? The video is really important. Is Java "pass-by-reference" or "pass-by-value"? I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, ) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're making. What is the explanation? When I open this ssh tunnel:  ssh -nXNT -p 22 localhost -L 0.0.0.0:8984:remote:8983   I get this error when trying to access the HTTP server running on localhost:8984:  channel 1: open failed: administratively prohibited: open failed   What does this error mean, and on which machine can you fix the problem? Find $cot\space x_n$ if $sin(x_{n+1}-x_n)+2^{-(n+1)}sin\space x_nsin \space x_{n+1}=0$ for $n\ge1$ Also Given     Suppose $x_1=tan^{-1}2&gt;x_2&gt;x_3&gt;......$ are positive real numbers   I cannot understand as to how I should approach but I have a hunch which is a bit hand-wavy way to prove it but tried to show that  $lim_{n \to \infty}x_n =\dfrac{\pi}{4}$ (For the proof see my edits in @hypernova's answer)  But can anybody prove it and then use it to find $cot \space x_n$ please?