Draw random number using Poisson distribution in Python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to mimic a chance encounter and I'm not sure how to do this. I would like to use a poisson distribution, but am open to other suggestions.
The idea is this: there is a central place where you can meet people. If there are 10 (the minimum) people at this place, the chance is 100% you will meet a specific person (e.g., chance = 1). If there are 6000 (the maximum) people at this place, the chance you will meet everyone is 3.5% you will meet a specific person (e.g. Chance = 0.035). How can I implement this using Python?

Not exactly sure if you can do Poisson distribution without μ.
This function will return a chance based on n, which is the number of people, and the given maximum and minimum values. In this calculation, the probability will be halved for each 1 point increase in the normalized value up to a certain point which is log(0.035)/log(0.5) or about 4.83650 because 0.5 ** 4.83650 ≈ 0.035. Meaning whenever n = maximum, the probability will be 0.035 and whenever n = minimum, the probability will be 1.
import math
def chance(n): # n is the number of people
maximum = 6000
minimum = 10
normalized = ((n - minimum) / (maximum - minimum)) * (math.log(0.035) / math.log(0.5))
probability = 0.5 ** normalized
return probability

Related

i need more decimal places for pi calculation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to make a Pi calculator in python but I need more decimal places.
it would help a lot if someone edited my code and carefully explained what they did.
this is the code I'm using.
import math
d = 0
ans = 0
display = 0
while True:
display += 1
d += 1
ans += 1/d**2
if display == 1000000:
print(math.sqrt(ans*6))
display = 0
# displays value calculated every 1m iterations
output after ~85m iterations: (3.14159264498239)
I need more than 15 decimal places (3.14159264498239........)
You’re using a very slowly converging series for π²∕6, so you are not going to get a very precise value this way. Floating point limitations prevent further progress after 3.14159264498239, but you’re not going to get much further in any reasonable amount of time, anyway. You can get around these issues by some combination of
micro-optimising your code,
storing a list of values, reversing it and using math.fsum,
using decimal.Decimal,
using a better series (like this one),
using a method that converges to the value of π quickly, instead of a series (like this one),
using PyPy, or a faster language than Python,
from math import pi.
you could try with a generator:
def oddnumbers():
n = 1
while True:
yield n
n += 2
def pi_series():
odds = oddnumbers()
approximation = 0
while True:
approximation += (4 / next(odds))
yield approximation
approximation -= (4 / next(odds))
yield approximation
approx_pi = pi_series()
for x in range(10000000):
print(next(approx_pi))

How does abs function work with this code : abs(3 + 4j)? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I already know how does abs function works.I mean it only shows you how far the number is from zero
The only thing i can't just get is with this example:
print(abs(3 + 4j)) # prints 5 !! Why ?
Because for complex numbers abs(number) will return magnitude of Complex Numbers.
Magnitude value will counted as :
√x2+y2 = √(3² + 4²) = √(9 + 16) = √(25) = 5.0
So abs with complex number will return magnitude of complex numbers.
for further reference you can use https://www.geeksforgeeks.org/abs-in-python/.
As the rest of the answers stated above, 3+4j is a complex number and the formula of calculating the absolute value of a complex number x+yi is sqrt( (x^2) + (y^2) ). In your case it's:
sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5
Because in python, 3+4j is complex number and python calculates the absolute value or magnitude of the complex number when you do abs() on it. Magnitude of 3+4j is 5. Try this :
type(3+4j)
It should give <class 'complex'>.
Note : Magnitude of a complex number a+bj is ((a**2+b**2)**0.5)

How do i figure out the multiple of a number python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
ok, I am studying python in my computing course and i have been challenged with designing a code which validates a GTIN-8 code, I have came to a hurdle that i can not jump. I have looked on how to find the equal or higher multiple of a 10 and i have had no success so far, hope you guys can help me!
Here is a small piece of code, i need to find the equal or higher multiple of 10;
NewNumber = (NewGtin_1 + Gtin_2 + NewGtin_3 + Gtin_4 + NewGtin_5 + Gtin_6 + NewGtin_7)
print (NewNumber)
The easiest way, involving no functions and modules, could be using floor division operator //.
def neareast_higher_multiple_10(number):
return ((number // 10) + 1) * 10
Examples of usage:
>>> neareast_higher_multiple_10(15)
20
>>> neareast_higher_multiple_10(21)
30
>>> neareast_higher_multiple_10(20.1)
30.0
>>> neareast_higher_multiple_10(20)
30
We can also make a generalized version:
def neareast_higher_multiple(number, mult_of):
return ((number // mult_of) + 1) * mult_of
If you need nearest lower multiple, just remove + 1:
def neareast_lower_multiple(number, mult_of):
return (number // mult_of) * mult_of
To find the nearest multiple, you can call both these functions and use that one with a lower difference from the original number.

Creating a function to generate a random number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Note: I cannot use any of the built in functions to solve this.
This is the question:
Given a function that returns a random integer number between 1 and 5, create a function that creates a random integer between 1 and 7.
As it has been answered in Adam Rosenfield's solution a while ago you may try to use an algoritm similar to this one below:
int i;
do
{
i = 5 * (rand5() - 1) + rand5(); // i is now uniformly random between 1 and 25
} while(i > 21);
// i is now uniformly random between 1 and 21
return i % 7 + 1; // result is now uniformly random between 1 and 7
EDIT: Translation to python:
def rand7():
i = 0;
while True:
i = 5 * (rand5() -1) + rand5()
if i > 21:
break
return i % 7 + 1
Apparently the following doesn't work. See the linked answer and the other responses as well.
Well, here is an approach I am thinking of. It could be wrong. I am going with the assumption that the result should be randomly distributed within the larger range.
Generate 7 random numbers using the given random functions (that generates numbers [1,5]) and calculate their sum.
This will result in a value between 7 (1 * 7) and 35 (5 * 7).
Because this value is evenly divisible by the target range and randomly distributed, then it seems like it would be valid to collapse the intermediate range [7,35] back to [1,7] without losing uniformity.
Or maybe that's not quite it - but it seems like exploiting a common multiple between the numbers is key.
import random
def winningFunc():
return originalFuncReturning1to5() + random.randint(0,2)

Pi calculation in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
n=iterations
for some reason this code will need a lot more iterations for more accurate result from other codes, Can anyone explain why this is happening? thanks.
n,s,x=1000,1,0
for i in range(0,n,2):
x+=s*(1/(1+i))*4
s=-s
print(x)
As I mentioned in a comment, the only way to speed this is to transform the sequence. Here's a very simple way, related to the Euler transformation (see roippi's link): for the sum of an alternating sequence, create a new sequence consisting of the average of each pair of successive partial sums. For example, given the alternating sequence
a0 -a1 +a2 -a3 +a4 ...
where all the as are positive, the sequences of partial sums is:
s0=a0 s1=a0-a1 s2=a0-a1+a2 s3=a0-a1+a2-a3 s4=a0-a1+a2-a3+a4 ...
and then the new derived sequence is:
(s0+s1)/2 (s1+s2)/2 (s2+s3)/2 (s3+s4)/2 ...
That can often converge faster - and the same idea can applied to this sequence. That is, create yet another new sequence averaging the terms of that sequence. This can be carried on indefinitely. Here I'll take it one more level:
from math import pi
def leibniz():
from itertools import count
s, x = 1.0, 0.0
for i in count(1, 2):
x += 4.0*s/i
s = -s
yield x
def avg(seq):
a = next(seq)
while True:
b = next(seq)
yield (a + b) / 2.0
a = b
base = leibniz()
d1 = avg(base)
d2 = avg(d1)
d3 = avg(d2)
for i in range(20):
x = next(d3)
print("{:.6f} {:8.4%}".format(x, (x - pi)/pi))
Output:
3.161905 0.6466%
3.136508 -0.1619%
3.143434 0.0586%
3.140770 -0.0262%
3.142014 0.0134%
3.141355 -0.0076%
3.141736 0.0046%
3.141501 -0.0029%
3.141654 0.0020%
3.141550 -0.0014%
3.141623 0.0010%
3.141570 -0.0007%
3.141610 0.0005%
3.141580 -0.0004%
3.141603 0.0003%
3.141585 -0.0003%
3.141599 0.0002%
3.141587 -0.0002%
3.141597 0.0001%
3.141589 -0.0001%
So after just 20 terms, we've already got pi to about 6 significant digits. The base Leibniz sequence is still at about 2 digits correct:
>>> next(base)
3.099944032373808
That's an enormous improvement. A key point here is that the partial sums of the base Leibniz sequence give approximations that alternate between "too big" and "too small". That's why averaging them gets closer to the truth. The same (alternating between "too big" and "too small") is also true of the derived sequences, so averaging their terms also helps.
That's all hand-wavy, of course. Rigorous justification probably isn't something you're interested in ;-)
That is because you are using the Leibniz series and it is known to converge very (very) slowly.

Categories

Resources