I wrote this code for my homework:
import random
score=[]
random.seed(1)
for i in range(0,100):
score.append(random.randrange(0,21))
for k in range(20, -1, -1):
print("Who get %2d score in test? : "%(k), end='')
while score.count(k)!=0:
j = score.index(k)
print("%3s" % (j), end=" ")
score.remove(k)
score.insert(j,25)
print("\n")
I ran it on my computer many times and the results were the same. Ironically, in other computers, the results are different from my computer, but also also getting repeated at each execution.
What's wrong with my code?
random.seed(n) starts the random number generator off from the same point each time the program is run.
That is you get the same sequence of random numbers. It is like taking a video of a dice being thrown and then playing it back each time - the numbers are random (pseudo random to be accurate) but you are playing back the sequence.
This is useful for testing, for example: you can run different versions of your program with the same random numbers, so you are sure differences in your results are only due to the program, not to chance.
Take random.seed out and you will get a sequence that starts at a random location. (On most computers, if you don't specify a seed, the clock time the program started is implicitely used as seed.)
When you write
random.seed(1)
You are saying to always use the same sequence of randomly generated numbers, therefore you always have the same results.
It happens when I run it on my computer too.
Simply remove the line and you should have different randomly generated numbers every time.
See this answer for an explanation of the seed.
And the python documentation for the random library
Related
Looking to prove a sibling wrong about how long it can take a computer to guess a specific string by using Brute Force even with the correct number of characters put in. I can get the code to run but I cannot figure out how to get it to print a new string every time it runs. I'm sure I'm over looking something simple. Below are a couple examples of the code I've tried.
import string
import random
random=''.join([random.choice(string.ascii_letters+string.digits) for n in xrange(5)])
while True:
if random != "Steve":
print(random)
if random == "Steve":
print("Found")
This will continually print the same string over and over. I've also tried this without the while statement just the if and it doesn't seem to work.
I know enough that once random picks those 5 randoms characters it won't change until something makes it change but like I said I'm not sure how to do that. I've tried moving random to different places but doesn't work I just get different error messages.
Can someone help me out.
random=''.join([random.choice(string.ascii_letters+string.digits) for n in xrange(5)])
This doesn't create a new random string each time. At this point random is just a randomly generated string that doesn't change while your while loop runs. Referencing random doesn't create a new string, but rather just gets the first string your generated, since random is just a string in your memory not a function.
Move the random string creation into a function:
import string
from random import choice
def make_random():
return ''.join([choice(string.ascii_letters+string.digits) for n in xrange(5)])
Then run the loop:
while True:
random = make_random()
if random != "Steve":
print(random)
if random == "Steve":
print("Found")
EDIT:
Switched import random to from random import choice because random (the variable) was overwriting random (the library) and throwing an attribute error when you try to call random.choice.
You have two problems here. As #Primusa pointed out, your random generation should be moved inside your loop otherwise it'll only run once. However, your other problem is that you're importing random and you're also setting a variable to random. This is where your NameError is coming from. You've defined random to be a string, which works on the first iteration of your loop. However, on the second iteration, random won't have a function called choice declared for it because it's a string at that point. Rename your random variable or import the random package under an alias, like this:
import random as rnd
Here is my code. I am just starting to learn Python.
I am trying to generate a random number and guess it. If the answer is 7, then my program will print "lucky". If not, then "unlucky". I try to run my simple code many times. Every time I get "unlucky". Is there anybody who knows whether the problem is in my algorithm or somewhere else. BTW, I really want to know how could I know specifically know what is the number randomly generated in python? I just wonder if the same number, that is being generated every time, is the same one or not.
from random import randint
z = input("What's the max number you want me to guess?")
choice = f"randint(1,{z})"
if choice == 7:
print("lucky")
else:
print("unlucky")
The reason you're getting unlucky every time has nothing to do with randomness.
Try running your code in the debugger, or adding a print(choice), to what what you're getting.
If you enter, say, 10, then choice is the string "randint(1,'10')". That string is never going to be equal to the number 7.
To make this work, you need to change two things:
Actually call randint, instead of making a string that looks like the source code to call it.
Call it on a number, like 10, not a string, like '10'.
So:
choice = randint(1, int(z))
Once you fix this, the random numbers will be random. Technically, they're created by a PRNG (pseudo random number generator), a fancy algorithm that takes a bunch of state information and spits out a sequence of numbers that look random, but are actually predictable from that state. But, as explained under seed, by default, Python seeds that generator with os.urrandom (which, on most platforms, is another PRNG, but is itself seeded by whatever actual random data is available).
If you want the sequence to be repeatable, for testing, you can call the seed function manually. For example, this program:
from random import randint, seed
seed(42)
z = input("What's the max number you want me to guess?")
choice = randint(1, int(z))
print(choice)
… will give you 2 every time you ask for a random number between 1 and 10.
If you want to check whether the same number is being guessed by random, just use print, to check it. Here:
from random import *
z = int(input("What's the max number you want me to guess?"))
choice = randint(1,z)
print("The number that I guessed:",choice)
if choice == z:
print("I gussed it! I got lucky.")
else:
print("I couldn't guess it, I got unlucky.")
I'm fairly new to coding, picking it up as a hobby, because its so far, fun, engaging, and challenging. Started out learning Python.
Just for the heck of it, I'm trying to write a function that will generate random integers, only print the even integers, and stop when it finally prints the number 28 (picked it at random.)
I've tried everything I could think of to get the function to loop, but the only thing I've been able to do successfully is just print out a single random even number once before the function stops.
Is there a way to have a function iterate through randint generated numbers between a user defined range, e.g.(0,x), and then apply if/elif/else's to the number(s) generated?
I've probably tried rewriting that same function 30-40 times, but I either get a single even number, a myriad of syntax errors, or some blue script that says "random.randint object at- and then a bunch of numbers.."???
Perhaps I just need to keep delving deeper into tutorials to find the answer, but this seemed like a more productive option. Perhaps there's something I'm not thinking of, or perhaps the concepts I need to employ I just haven't learned yet. Any advice/tips/actual lines of code GREATLY APPRECIATED!
Thanks in advance
Mike
Turns out, I was using the for loop incorrectly, and placing the num = randint(0,x) in the wrong place(wrong line). I've included the final function:
from random import randint
def random_even(x):
for i in range(x):
num = randint(0,x)
if num==28:
print(num,"\n DONE")
break
elif num%2==0:
print(num)
I've been working on the following code which sort of maximizes the number of unique (in lowest common denominator) p by q blocks with some constraints. It is working perfectly. For small inputs. E.g. input 50000, output 1898.
I need to run it on numbers greater than 10^18, and while I have a different solution that gets the job done, this particular version gets super slow (made my desktop reboot at one point), and this is what my question is about.
I'm trying to figure out what is causing the slowdown in the following code, and to figure out in what order of magnitude they are slow.
The candidates for slowness:
1) the (-1)**(i+1) term? Does Python do this efficiently, or is it literally multiplying out -1 by itself a ton of times?
[EDIT: still looking for how operation.__pow__ works, but having tested setting j=-j: this is faster.]
2) set instantiation/size? Is the set getting too large? Obviously this would impact membership check if the set can't get built.
3) set membership check? This indicates O(1) behavior, although I suppose the constant continues to change.
Thanks in advance for insight into these processes.
import math
import time
a=10**18
ti=time.time()
setfrac=set([1])
x=1
y=1
k=2
while True:
k+=1
t=0
for i in xrange(1,k):
mo = math.ceil(k/2.0)+((-1)**(i+1))*(math.floor(i/2.0)
if (mo/(k-mo) not in setfrac) and (x+(k-mo) <= a and y+mo <= a):
setfrac.add(mo/(k-mo))
x+=k-mo
y+=mo
t+=1
if t==0:
break
print len(setfrac)+1
print x
print y
to=time.time()-ti
print to
I am attempting to create a very basic Combat Simulator. Here is my code:
import random
#from random import * I had, at one time, created a D20 generator that used the * function. I have since removed it in favor of using randint()
sword = 0
D20 = 0
class Hero:
def swing_and_hit(sword, D20):
sword = D20
print(D20)
if sword >= 10:
print("You have defeated the Villian!")
elif sword < 10:
print("You have died!")
D20 = randint(1,20)
Adventurer = Hero
#Adventurer.D20_Func() Ignore this...aborted effort to try something
Adventurer.swing_and_hit(sword, D20)
Every time I run this at http://pythontutor.com/visualize.html#mode=edit, the random number generator outputs 13. I cannot figure out why it does this...any thoughts? I have tried nesting the random number generation inside the function swing_and_hit, and it still generates the same number every time I run the program.
This seems to be a "feature" in Pythontutor to ensure different people will see the same results running the same code. (I am getting always 17)
Whether or not they define it as a feature, it is broken as it could be - random numbers should be random.
Just install Python in your own computer, and get going - it is a smaller than 20MB download from http://python.org if you are using Windows (and if you are not, you have Python installed already). There are interactive interpreters and even a simple development environment that is installed along with the language - there is no motive for you to hurt yourself with a web implementation that might be good for group study of some code, or introspecting what goes in each variable step by step. Python interactive environments are much more dynamic than the "click to run" you get on this site.
I think the random number generator producing the same number every time is a quirk of pythontutor.com. I get 17 every time with just this code:
import random
print(random.randint(1, 20))
If you must use a website to run your code, try repl.it.