probability

Using random number generator to generate a number with a given expected value

家住魔仙堡 提交于 2019-12-11 03:36:51
问题 I came across the following question: Using rand() function, generate a number with expected value k. Options are: 1) int GetRandom(int k) { v=0; while(rand()<1.0f/(float)k) v++; return v; } 2) int GetRandom(int k) { v=0; while(rand()<(1-1.0f/(float)k)) v++; return v; } 3) int GetRandom(int k) { v=0; while(rand() > (1-1.0f/(float)(k+1))) v++; return v; } 1) seemed like the correct answer. Examining the outcome for specific values of k seems to indicate this is the not the case. (I set k=3 .

Simulating weighted random number - Java

心不动则不痛 提交于 2019-12-11 03:07:31
问题 I looked at multiple stack overflow articles and couldn't find a reasonable response. If there is a duplicate please indicate. I have a list of items. Something like: String giantRat []={"Bandage", "Healing Potion", "Minor Healing Potion", "Rat Teeth", "Fur", "Rat Tail", ""}; This indicates the possible items this giantRat can drop. There is a correlated array with matching index that holds what I would hope is the weighted probability of the occurence. Something like: int giantRatDropRate[]=

Python Custom Zipf Number Generator Performing Poorly

旧街凉风 提交于 2019-12-11 02:46:00
问题 I needed a custom Zipf-like number generator because numpy.random.zipf function doesn't achieve what I need. Firstly, its alpha must be greater than 1.0 and I need an alpha of 0.5 . Secondly, its cardinality is directly related to the sample size and I need to make more samples than the cardinality, e.g. make a list of 1000 elements from a Zipfian distribution of only 6 unique values. @stanga posted a great solution to this. import random import bisect import math class ZipfGenerator: def _

Simulate samples from a joint cumulative distribution function?

限于喜欢 提交于 2019-12-11 02:25:59
问题 I have a joint density function for two independent variables X and Y. And I now want to sample new x,y from this distribution. What I believe I have to do is to find the joint cumulative distribution and then somehow sample from it. I kinda know how to do this in 1D, but I find it really hard to understand how to do it in 2D. I also used the matlab function cumtrapz to find the cumulative distribution function for the above pdf. Just to be clear, what i want to do is to sample random values

Inverted beta in MySQL

空扰寡人 提交于 2019-12-11 02:14:50
问题 I need to implement an inverted beta function in MySQL (similar to Excel's BETAINV). There is some related material is available on Wolfram MathWorld's Beta Distribution page. Any clues on where to start implementing this functionality in MySQL? 回答1: Look at the cephes library, specifically cprob.tgz . Be warned that the licensing situation of that code seems to be unclear. The source code just says "free", which was a problem for Debian so apparently they got the author to relicense it under

Defining two random variables that depend on a single condition

半城伤御伤魂 提交于 2019-12-11 02:12:23
问题 In sympy, how can I define two random variables, X and Y, that depend on a common condition? For example, how do I solve a problem such as the following: We throw a dice. If it falls on 1, then X=1 and Y=0. If it falls on 2, then X=0 and Y=1. Otherwise, X=Y=0. What is the covariance of X,Y? 回答1: If X and Y are functions of some Z, then create Z and define X, Y through it. Piecewise helps with this: from sympy.stats import * Z = Die("Z", 6) X = Piecewise((1, Eq(Z, 1)), (0, True)) Y = Piecewise

Russian Roulette with adding new bullets in R

自古美人都是妖i 提交于 2019-12-11 01:57:04
问题 I want to write a program in R for modificated Russian roulette. The first player has a revolver with 1 bullet. If he survive, we add one bullet to a chamber and spin the barrel. And so on, until the sixth player (if the others survive). I want to know a probability, that the revolver will come to the sixth player and I want to make it like a simulation. I know how to make a program for classical russian roulette roulette <- function(numshots) { killed = 6 killshots = 0 won = 0 i = 0 while

How to create a random variable whose parameters are themselves random variables in SymPy?

*爱你&永不变心* 提交于 2019-12-10 23:01:32
问题 I have a random variable Y whose distribution is Poisson with parameter that is itself a random variable X, which is Poisson with parameter 10. How can I use SymPy to automatically calculate the covariance between X and Y? The code from sympy.stats import * x1 = Poisson("x1", 3) x2 = Poisson("x2", x1) print(covariance(x2,x1)) raises an error ValueError: Lambda must be positive The documentation is not clear to me on this matter, and playing around with the function given did not seem to work.

Probability of getting the same value using Math.random

爱⌒轻易说出口 提交于 2019-12-10 21:28:29
问题 The requirement is to send a unique id to database when user click on submit button. So I am using Javascript Math.random method. I just want to know what are the chances or possibility to get same number and what bit size of using Math.random . 回答1: You're up against something called the birthday problem: even though there are 366 possible birthdays, when you get only 26 people in a room, the chance that some pair will have the same birthday is better than 50-50. In general, collisions are

Simulate coin toss for one week?

五迷三道 提交于 2019-12-10 16:34:45
问题 This is not homework . I am interested in setting up a simulation of a coin toss in R. I would like to run the simulation for a week. Is there a function in R that will allow me to start and stop the simulation over a time period such as a week? If all goes well, I may want to increase the length of the simulation period. For example: x <- rbinom(10, 1, 1/2) So to clarify, instead of 10 in the code above, how do I keep the simulation going for a week (number of trials in a week versus set