Does random.randint have a cooldown (or so) time before it generates a new random/seed?
As a part of something bigger I am trying to program a list of 5 "dice rolls" (random numbers from 1 to 6). I have this code in my program:
from random import randint
rolls = [randint(1,6),randint(1,6),randint(1,6),randint(1,6),randint(1,6)]
print (rolls)
When checking things i rolled 1,2,3,4,5 twice in a row and then i checked other rolls and the numbers didnt seem to change much between each roll
Related
I am trying to write a program that generates a random number from a random number generated by a Perlin noise. If you ask why I am trying to add a little bit more randomness to a random number generator and see how that would work.
Anyways my problem is from the Perlin noise random number I always get 12 digit random numbers like: 124592051214, 431268750000, 420799999999, 613979257812...
the thing is I want this function to be used just like a normal python Random function. You give the borders(a,b) and you get a random number in those borders.
So, how can I turn a 12 digit number to match the given borders? Thanks in advance
ex:
num = 124592051214
perlinRand(50,100)
62
You can do something like
perlin_noise % (higher-lower) + lower
where "higher" is the higher limit of the random number you want, "lower" is the lower limit and perlin_noise is the number you generate
def perlinRand(lower, higher):
perlin_noise = get_perlin_noise_random_number()
return perlin_noise % (higher-lower) + lower
perlinRand(50, 100)
I have been trying but struggling to make a rolling dice simulator in Python that is an assignment for school and am looking for some help for it. These are the instructions:
This project involves writing a program that simulates rolling a dice.
When the program runs, it will randomly choose a number between 1 – 6.
Have the dice roll 100 times. The program will count if it is a 1 or 2
or 3 or 4 or 5 or 6. After rolling the dice 100 times the program will
output how many times each number was rolled. It should then ask you
if you’d like to roll again, answer (1 for Yes) and (0 for No).
What you will be Using : Variables Integer Random Print For Loop
and/or While Loop
I have two sections of code that I need to loop over:
import random
for x in range(100):
value = random.randint(1, 6)
print(value)
and:
value = input("Wanna roll? 1 yes 2 no:\n")
print(f'You entered {value}')
y={value}
while y=1
print(value)
What is an idiomatic way of doing this in Python?
I just picked up Python as my second language a few days ago and I managed to create a little program that asks the user for a random number and the computer generates random numbers until that random number equals the user's number.
import random
import time
print("Enter a random number. I will try to find it")
password = int(input() )
start = time.time()
for i in range(2 ** 10):
rand_num = random.randint(0, 100)
if rand_num == password:
print("Number found!", password)
end = time.time()
print(" It took", end - start, "seconds to find the number")
break
else:
print(rand_num, "is not the number")
To step things up, I want to create like a visual representation of all the randomly generated numbers on a number line.
Something like this:
I have tried looking on Google but haven't gotten any success.
Thanks
You will need to store the values generated while searching for the number entered by the user.
Have a read about lists and dictionaries. You could store in a list all the numbers generated while searching, and then aggregate the results for display once you find it.
Or you could use a dictionary to store a count of the numbers that fall in to ranges such as 0-10, 10-20, etc.
Creating the graph would be another question all together. Have a go at solving the first part, aggregating the results, and then worry about the visual display.
Trying to create a Random Number Generator that depending on the number outputs a statement. I keep getting the output of Over 50 no matter what. I can't seem to figure out why though.
from random import randrange
print randrange(100)
number = randrange
if number <= 49:
print"Under 50"
else:
print"Over 50"
Changing the following line corrected the issue.
number = randrange(100)
This statement doesn't do what you think:
number = randrange
It's setting number to the function, not to a result from calling the function. If you now did print number(100) you'd see what I mean. Python 2 allows you to compare arbitrary values to each other, even if they're not really comparable, so the <= doesn't fail but doesn't deliver a result that makes sense either. Python 3 would generate an error if you tried to do the same thing.
As an exercise I'm writing a program to calculate the odds of rolling 5 die with the same number. The idea is to get the result via simulation as opposed to simple math though. My program is this:
# rollFive.py
from random import *
def main():
n = input("Please enter the number of sims to run: ")
hits = simNRolls(n)
hits = float(hits)
n = float(n)
prob = hits/n
print "The odds of rolling 5 of the same number are", prob
def simNRolls(n):
hits = 0
for i in range(n):
hits = hits + diceRoll()
return hits
def diceRoll():
firstDie = randrange(1,7,1)
for i in range(4):
nextDie = randrange(1,7,1)
if nextDie!=firstDie:
success = 0
break
else:
success = 1
return success
The problem is that running this program with a value for n of 1 000 000 gives me a probability usually between 0.0006 and 0.0008 while my math makes me believe I should be getting an answer closer to 0.0001286 (aka (1/6)^5).
Is there something wrong with my program? Or am I making some basic mistake with the math here? Or would I find my result revert closer to the right answer if I were able to run the program over larger iterations?
The probability of getting a particular number five times is (1/6)^5, but the probability of getting any five numbers the same is (1/6)^4.
There are two ways to see this.
First, the probability of getting all 1's, for example, is (1/6)^5 since there is only one way out of six to get a 1. Multiply that by five dice, and you get (1/6)^5. But, since there are six possible numbers to get the same, then there are six ways to succeed, which is 6((1/6)^5) or (1/6)^4.
Looked at another way, it doesn't matter what the first roll gives, so we exclude it. Then we have to match that number with the four remaining rolls, the probability of which is (1/6)^4.
Your math is wrong. The probability of getting five dice with the same number is 6*(1/6)^5 = 0.0007716.
Very simply, there are 6 ** 5 possible outcomes from rolling 5 dice, and only 6 of those outcomes are successful, so the answer is 6.0 / 6 ** 5
I think your expected probability is wrong, as you've stated the problem. (1/6)^5 is the probability of rolling some specific number 5 times in a row; (1/6)^4 is the probability of rolling any number 5 times in a row (because the first roll is always "successful" -- that is, the first roll will always result in some number).
>>> (1.0/6.0)**4
0.00077160493827160479
Compare to running your program with 1 million iterations:
[me#host:~] python roll5.py
Please enter the number of sims to run: 1000000
The odds of rolling 5 of the same number are 0.000755