My goal is to generate a series of random numbers without allowing the same number repeating next to eachother but allowing the same number twice like 1,2,1,2 but not 1,1,2,2 and I'm just not really sure how to accomplish this.
Something like this?
import random
list = []
list.append(random.randrange(50))
for i in range(50):
x = random.randrange(50)
while x == list[i]:
x = random.randrange(50)
list.append(x)
print(list)
Also you should post your own attempt. It gives everyone a good reference and starting point to help you out in a meaning full and focused way.
Related
i coded 6number from1 to 45 without duplicating
from random import *
lotto = []
while True:
m = randrange(1, 46)
if m not in lotto:
lotto.append(m)
if len(lotto) == 6:
break
but I want to make different 6number as wanting
example, if i input 2 {1,21,23,43,35,26} {21,15,6,37,28,7}
but with this code,
for i in range(init(input""))
print(lotto)
same result is printed
i wanna different result
how do i add it?
you can put the lotto genrator in a function :
from random import randrange
def give_lotto():
lotto = []
while True:
m = randrange(1, 46)
if m not in lotto:
lotto.append(m)
if len(lotto) == 6:
break
return(lotto)
then print( result in the loop) in your example it gives you the same result because it doesn't generate it again it just prints the final result twie that's why putting it into a function will give your intended result
for i in range(int(input())):
print(give_lotto())
If I understand correctly you are wanting to produce a list of n lotto combinations
import random
def n_lotto(n):
lotto_draws = []
for i in range(n):
balls = random.sample(range(1, 46), 7)
lotto_draws.append(balls)
return lotto_draws
As a side note as you appear to be new a quick FYI. Python is a slow language butit has a lot of powerful builtin functions most of which are written in C and are therefore blazing fast, they are also extremely well tested meaning that it is almost always a good idea to have a quick google for a builtin if what you are trying to do is relatively simple as:
it'll be easier to read
it'll be faster
you're less likely to have bugs
I'm not completely sure what you're after but as a note using
lotto.append(m.pop())
in place of
if m not in lotto:
lotto.append(m)
Is faster and removes the poped character from m (preventing any logic errors in the future)
edit:
Apologies I haven't used the random module in a while and I thought randrange was generating a list not a number, therefore pop will not work and the original implementation is correct. However, for this purpose random.sample is a far better function to use
I’ve been working on a school project and need to do a loop consisting of a couple random numbers but I need them to output a different number each time. The code I’ve been using for the random numbers is this.
import random
a=random.randint(1,9)
I’m new to coding and just starting getting into python for fun and have looked everywhere for how to complete this loop but I can’t find anything that works.I know this code does not include a loop and the loop I was using before was “while True” and “for i in range” Thanks
You have not created any loop yet. You're generating random integer only once.
In order to generate more of them you have to use something like a for loop.
If you're familiar with the concept of range then this is a simple example of generating x-number of random integers.
import random
x = 10
for i in range(0, x):
a = random.randint(1, 9)
print(a)
I am assuming you are doing something like this. Your loop needs to call the random.randint function on each iteration of the loop.
a = random.randint(1,9)
for i in range(5):
print(a)
What you should be doing is this
for i in range(5):
print(random.randint(1,9))
If you want to have 5 values between 1 - 9 without repetition you can use the sample function of the random module. This will select n values from a list and store them in a new list.
array = random.sample(range(1, 9), 5)
for elem in array:
print(elem)
But if you want to have a new random value between 1 and 9 every iteration and you dont care if a number is repeated I would go with the answers the others already gave you.
Random works with a seed, with the same seed the same output
Easiest way to achieve that you want, you will have a different seed each time you run your program
import random
from datetime import datetime
random.seed(datetime.now())
for _ in range(10):
a=random.randint(1,9)
I want a random generator that does not generate any numbers generated before. Calling this generator does not return a list. It returns 1 number every time and the number will be a totally new number not generated before.
My idea of implementing this so far is to generate a long non-repeating random list first and then retrieve from the list one by one.
Using Python, but I guess this is more of a general logic question.
edit:
Sorry for the confusion everyone. Clarification on the problem I am trying to implement: I am developing an app where a user can sign up and receive a hotel room number. The app has some hotels setup. Each new user will receive a unique hotel number of the hotel. The range will probably be from 0001 to 9999. My goal of this is to have an algorithm spit out a random number from 0001 to 9999 for that user, and this number has to be unique so that each room number of a hotel is occupied by one user only. I am using Django.
If you already know the numbers you need to select from,
import random
x = [4,3,6,7,2,...] # list of desired numbers
result = x.pop(random.randint(0, len(x) - 1))
You can do something like:
import numpy as np
random_nums = []
for _ in range(10):
while True:
x = np.random.randint(0,10)
if x not in random_nums:
random_nums.append(x)
break
Probably a simple answer, not sure what I am missing. For a homework assignment I have to use random.random() to generate numbers between 30 and 35. The seed has to be set to 70 to match the pseudo-random numbers with the grader. This wasn't in my lecture so I am a little stumped as to what to do.
I have:
import random
def problem2_4():
print(random.random(30,35))
But this is clearly wrong.
The assignment says the output should look like (note: for the problem i use def problem2_4() just for the assignment grading system)
problem2_4()
[34.54884618961936, 31.470395203793395, 32.297169396656095, 30.681793552717807,
34.97530360173135, 30.773219981037737, 33.36969776732032, 32.990127772708405,
33.57311858494461, 32.052629620057274]
The output [blah, blah, blah] indicates that it is a list of numbers rather than a series of numbers printed one-by-one.
In addition, if you want random floating point values, you'll need to transform the numbers from random.random (which are zero to one) into that range.
That means you'll probably need something like:
import random # Need this module.
def problem2_4():
random.seed(70) # Set initial seed.
nums = [] # Start with empty list.
for _ in range(10): # Will add ten values.
nums += [random.random() * 5 + 30] # Add one value in desired range.
print(nums) # Print resultant list.
Of course, the Pythonic way to do this would be:
import random
random.seed(70)
print([random.random() * 5 + 30 for _ in range(10)])
Bit that might be a bit ahead of where your educator is working. Still, it's good to learn this stuff as early as possile since you'll never be a Pythonista until you do :-)
The function that you are looking for is randint, which returns an integer (whole number) between the specified minimum and maximum values.
So the solution would be
random.randint(30, 35)
Python Docs
In my code I am attempting to generate 8 random numbers using a for loop. I then add these to the end of my array of the name 'numbers'. Now I will like to add the numbers in this array together but I can't figure out a way to do this.
Below you will see my code.
def get_key():
numbers = []
for i in range(8):
i = random.randrange(33, 126)
numbers.append(i)
get_key()
You want to use sum
a = [1,2,3,4,5]
sum(a) # outputs 15
add as in sum? simply do sum(numbers).
As others have noted, you can use sum to iterate and accumulate over the list (the default accumulator for sum is int(), i.e. 0). Also, if this is the only purpose for the list, you can save memory by using a generator.
import random
get_key = lambda: sum(random.randrange(33, 126) for _ in range(8))
print( get_key() ) # 612
The real question is why are you trying to do this? It seems like there will be a more direct method by using a higher-level distribution. For example, the sum of n I.I.D. variables will approach a Normal distribution.