probability

Will this give me proper random numbers based on these probabilities? C++

烂漫一生 提交于 2019-12-04 08:23:57
Code: int random = (rand() % 7 + 1) if (random == 1) { } // num 1 else if (random == 2) { } // num 2 else if (random == 3 || random == 4) { } // num 3 else if (random == 5 || random == 6) { } // num 4 else if (random == 7) { } // num 5 Basically I want each of these numbers with each of these probabilities: 1: 1/7 2: 1/7 3: 2/7 4: 2/7 5: 1/7 Will this code give me proper results? I.e. if this is run infinite times, will I get the proper frequencies? Is there a less-lengthy way of doing this? derobert Not, it's actually slightly off, due to the way rand() works. In particular, rand returns

Generating random numbers with weighted probabilities in python

泄露秘密 提交于 2019-12-04 07:29:33
Given a positive integer array a , the goal is to generate 5 random numbers based on the weight they have in the array. For example: a = [2,3,4,4,4,4,4,6,7,8,9] In this case the number 4 has appeared 5 times, in this case the number 4 should have the probability of 5/11 to appear. No numbers should be repeated. Given a , an array of positive integers, you'll first need to compute the frequency of each integer. For example, using bincount : >>> a = [2,3,4,4,4,4,4,4,5,6,7,8,9,4,9,2,3,6,3,1] >>> b = np.bincount(a) b tells you the frequency of each integer in a . The corresponding set of weights

can rand() be used to generate predictable data?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 06:45:37
问题 My goal is generate 2D or 3D geometry without having to store it on disk, so my goal is to have any sort of function than generate the same values according to a small seed. I don't mean to seek random values, but if the same "random" garbage data is returned when given the same seed, that's something I'm looking for. If I give srand() the same integer, I get the same sequence out of rand(). Is that an intended feature ? If not, are there known standard functions designed to do the same thing

Generate a set of M elements from an array of size N

六眼飞鱼酱① 提交于 2019-12-04 05:05:40
问题 UPDATE: according to the comments let's make some clarifications. I'm trying to understand solution for the following task: Randomly generate a set of M elements from an array of size N. Each element must have equal probability of being chosen. I found the following solution (I've already read this question, but it does not answer my question): int rand(Random random, int min, int max) { return random.nextInt(1 + max - min) + min; } char[] generateArray(char[] original, int subsetSize) { char

Get true or false with a given probability

痴心易碎 提交于 2019-12-04 04:53:33
I'm trying to write a function in c++ that will return true or false based on a probability given. So, for example if the probability given was 0.634 then, 63.4% of the time the function would return true. I've tried a few different things, and failed. Any help? If you'd like to do this in C++11, you can use its various random number engines, combined with the uniform_real_distribution to provide a good result. The following code demonstrates: #include <random> std::knuth_b rand_engine; // replace knuth_b with one of the engines listed below std::uniform_real_distribution<> uniform_zero_to_one

Calculating Probability of a Random Variable in a Distribution in Python

那年仲夏 提交于 2019-12-04 04:35:26
Given a mean and standard-deviation defining a normal distribution , how would you calculate the following probabilities in pure-Python (i.e. no Numpy/Scipy or other packages not in the standard library)? The probability of a random variable r where r < x or r <= x. The probability of a random variable r where r > x or r >= x. The probability of a random variable r where x > r > y. I've found some libraries, like Pgnumerics , that provide functions for calculating these, but the underlying math is unclear to me. Edit: To show this isn't homework, posted below is my working code for Python<=2.6

How do I generate a random vector (0,1) with a known probability in MATLAB

一曲冷凌霜 提交于 2019-12-04 04:15:34
问题 I am using the following code operation=[rand(1,noOfNodes)>prob]; to generate 1 and zeros ( noOfNodes times). If I use prob=0.2 and try 100 values there exist in some cases 40 zeros. Isn't it weird? I need the probability of getting zeros less than 0.2 回答1: No, that's not weird. That's probability for ya. If you flip a coin 100 times, you don't always get 50 heads and 50 tails. Sometimes you get 49 and 51, and on that rarest of occasions you can even get the same one 100 times. With your

Homework: Simulating coin tosses until consecutive heads using R

谁说胖子不能爱 提交于 2019-12-04 04:11:26
问题 This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 5 years ago . I am new to R hence asking here (haven't been able to find very helpful tutorials for simulation that are detailed.) The problem statement is this Simulate a coin toss for 20 times and record the number of heads & longest run of heads. Simulate a coin toss and record the number of flips necessary until 2,3,4 heads occur in sequence (consecutively) (negative binomial?) Make 100

multinomial pmf in python scipy/numpy

孤街浪徒 提交于 2019-12-04 00:25:21
Is there a built-in function in scipy/numpy for getting the PMF of a Multinomial? I'm not sure if binom generalizes in the correct way, e.g. # Attempt to define multinomial with n = 10, p = [0.1, 0.1, 0.8] rv = scipy.stats.binom(10, [0.1, 0.1, 0.8]) # Score the outcome 4, 4, 2 rv.pmf([4, 4, 2]) What is the correct way to do this? thanks. There's no built-in function that I know of, and the binomial probabilities do not generalize (you need to normalise over a different set of possible outcomes, since the sum of all the counts must be n which won't be taken care of by independent binomials).

How to predict survival probabilities in R?

旧城冷巷雨未停 提交于 2019-12-03 21:32:04
问题 I have data called veteran stored in R. I created a survival model and now wish to predict survival probability predictions. For example, what is the probability that a patient with 80 karno value, 10 diagtime , age 65 and prior=10 and trt = 2 lives longer than 100 days? In this case the design matrix is x = (1,0,1,0,80,10,65,10,2) Here is my code: library(survival) attach(veteran) weibull <- survreg(Surv(time,status)~celltype + karno+diagtime+age+prior+trt ,dist="w") and here is the output: