I would like to generate a random number between x and y with a known probability. For example, using Output=randint(0,2) i know that there is a 33% probability that Output is equal to 1.
Similarly, if Output=randint(0,3) i know that there is 25% probability that the Output is equal to 1.
How can i generate a random number similar to above to make sure that there is 40% probability that Output equal to 1?
Thanks a lot
Not sure what's your aim here, how about getting 40% by giving a fixed range, and than manipulating the results?
For example, this will give you 40% value of 1, and 30% of 2 or 3
num = randint(0,9)
if num <= 3:
num = 1
elif num <= 6:
num = 2
else:
num = 3
Here I have an alternative solution, looks shorter and since there are no if statements it should be much faster.
Also, you can easily change what number is to be returned with the 60% probability
a = [1,1,0,0,0]
num = a[randint(0,4)]
As alternative here is a version of the same with a single line:
num = list((1,1,0,0,0))[randint(0,4)]
Related
I'm trying to calculate a weighted median, but don't understand the difference between the following two methods. The answer I get from weighted.median() is different from (df, median(rep(value, count))), but I don't understand why. Are there many ways to get a weighted median? Is one more preferable over the other?
df = read.table(text="row count value
1 1. 25.
2 2. 26.
3 3. 30.
4 2. 32.
5 1. 39.", header=TRUE)
# weighted median
with(df, median(rep(value, count)))
# [1] 30
library(spatstat)
weighted.median(df$value, df$count)
# [1] 28
Note that with(df, median(rep(value, count))) only makes sense for weights which are positive integers (rep will accept float values for count but will coerce them to integers). This approach is thus not a full general approach to computing weighted medians. ?weighted.median shows that what the function tries to do is to compute a value m such that the total weight of the data below m is 50% of the total weight. In the case of your sample, there is no such m that works exactly. 28.5% of the total weight of the data is <= 26 and 61.9% is <= 30. In a case like this, by default ("type 2") it averages these 2 values to get the 28 that is returned. There are two other types. weighted.median(df$value,df$count,type = 1) returns 30. I am not completely sure if this type will always agree with your other approach.
You are working at the cash counter at a fun-fair, and you have different types of coins available to you in infinite quantities. The value of each coin is already given. Can you determine the number of ways of making change for a particular number of units using the given types of coins?
counter = 0
def helper(n,c):
global counter
if n == 0:
counter += 1
return
if len(c) == 0:
return
else:
if n >= c[0]:
helper(n - c[0], c)
helper(n,c[1:])
def getWays(n, c):
helper(n,c)
print(counter)
return counter ```
#the helper function takes n and c
#where
#n is amount whose change is to be made
#c is a list of available coins
Let n be the amount of currency units to return as change. You wish to find N(n), the number of possible ways to return change.
One easy solution would be to first choose the "first" coin you give (let's say it has value c), then notice that N(n) is the sum of all the values N(n-c) for every possible c. Since this appears to be a recursive problem, we need some base cases. Typically, we'll have N(1) = 1 (one coin of value one).
Let's do an example: 3 can be returned as "1 plus 1 plus 1" or as "2 plus 1" (assuming coins of value one and two exist). Therefore, N(3)=2.
However, if we apply the previous algorithm, it will compute N(3) to be 3.
+------------+-------------+------------+
| First coin | Second coin | Third coin |
+------------+-------------+------------+
| 2 | 1 | |
+------------+-------------+------------+
| | 2 | |
| 1 +-------------+------------+
| | 1 | 1 |
+------------+-------------+------------+
Indeed, notice that returning 3 units as "2 plus 1" or as "1 plus 2" is counted as two different solutions by our algorithm, whereas they are the same.
We therefore need to apply an additional restriction to avoid such duplicates. One possible solution is to order the coins (for example by decreasing value). We then impose the following restriction: if at a given step we returned a coin of value c0, then at the next step, we may only return coins of value c0 or less.
This leads to the following induction relation (noting c0 the value of the coin returned in the last step): N(n) is the sum of all the values of N(n-c) for all possible values of c less than or equal to c0.
Happy coding :)
So I want to make it that for how high a number is, the output is higher. I do not know how to do that.
I have done it so for the amount of numbers given, it gets multiplied by 2, but I want it for how high the number is ( 2x2, 4x2) it gives a higher output.
import random
speed = random.randint(1,4)
power = 1
for x in range(speed):
power *2
print(power)
So I want a random number to be picked from 1 to 4 which is speed. Then I want to multiply it by 2 for how high the value of speed is, for example, if speed is 3, then the power will multiply by 6, so the power then is 18, but what I get is just 1 printed 1-4 times. I do not know what to do.
import random
speed = random.randint(1,4)
power = 1
for x in range(speed):
p = power *2
print(p)
Assign your equation result as a variable and print that out instead.
This is the source of the problem at uva.onlinejudge.org
The problem basically says:
Given N amount of money which has to be given!! we need to find out how much minimum coins we can give and the total value of those coins such that the extra amount given is minimal using n given denominations!
For example:
1400 -> N
3 -> no of denominations
500
1000
2000
Output: 1500 2
My question is:
What are the overlapping subproblems here?
I mean:
Are there any overlapping subproblems?
Because I couldn't find any...
The overlapping sub-problems are the minimum number of coins/bills to sum to a particular number of cents.
for coin_value in coins(sorted)
for sum where num[sum] is valid
num[ sum + coin_value ] = Min( num[sum + coin_value], num[sum] + 1 )
See: Dynamic Programming Coin Change Limited Coins
The constraints to the problem are low enough you can use the accepted answer to that question. The only difference is that you calculate the minimum number of coins/bills to sum up to the target price, and then you continue going past the target price. When you are done filling in the array, start at the target price and go up until you find a valid answer.
Sort the coins/bills and start with the largest denomination and go down.
(My solution was accepted on UVa.)
def exp3(a,b):
if b == 1:
return a
if (b%2)*2 == b:
return exp3(a*a, b/2)
else: return a*exp3(a,b-1)
This is a recursive exponentiator program.
Question 1:
If b is even, it will exceute (b%2)2 == b. If b is odd, it will exceute aexp3(a,b-1). There is no problem in my program. If b is 4, (4%2)*2=0, and 0 is not equal to b. So I can't understand how to calculate b when it's even.
Question 2:
I want to calucate the number of steps in the program. so according to my textbook, I can get the formual as follows.
b even t(b) = 6 + t(b/2)
b odd t(b) = 6 + t(b-1)
Why is the first number 6? How can I get the number 3 in the beginning?
Your (b%2)*2 == b test is never true. I think you want b % 2 == 0 to test if b is even. The code still gets the right answer because the other recursive case (intended only for odd b values) works for even ones too (it's just less efficient).
As for your other question, I have no idea where the 6 is coming from either. It depends a lot on what you're counting as a "step". Usually it's most useful to discuss performance in terms of "Big-O" values rather than specific numbers.