Python random number loop with different outcomes each time - python

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)

Related

Replicate Random Numbers from Visual Basic with Python

I have a code in Visual Basic that generates a vector of random numbers for a given seed (456 in my case). I need to replicate that code in Python and I am thinking if it is possible to generate with Python the same vector of random numbers, that is, to select the same seed as in VBA.
Let me show an example:
In VBA I have the following code:
Function rnd_seed(seed)
Dim x(1 To 10) As Double
Rnd (-1)
Randomize seed
For i = 1 To 10
x(i) = Rnd
Next i
rnd_seed = x
End Function
With this function, I obtain the values (for seed 456):
0.014666617
0.462389946
0.098651111
0.189074159
0.107685387
0.219710588
0.967558324
0.409745097
0.213494837
0.848815441
In Python, I use the following code (as suggested by K-D-G):
seed = 456
import random
random.seed(seed)
arr=[]
for i in range(10):
arr.append(random.random())
With this Python code, I obtain the following values:
[0.7482025358782363,
0.9665873085424435,
0.4352093219057409,
0.7942997804992433,
0.6481497216250237,
0.6174050474978059,
0.8222710780743806,
0.7895737180242367,
0.8864808985728122,
0.3264489135810307]
I see that the values obtained for VBA and Python are different. My question is if it is possible to generate with Python the same random numbers that I have generated with VBA. Is there any way to map the VBA and Python seeds?
Many thanks in advance!
Something like this:
import random
random.seed(seed)
arr=[]
for i in range(n):
arr.append(random.random())
Note random.random() returns value between 0 and 1 if you want integers over a range use random.randint(start, stop)

how do i print eachother while sentence?

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

Can I randomly generate numbers without the same number next to eachother?

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.

Use Random.random [30,35] to print random numbers between 30,35. Seed(70)

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

Creating random list of number in python within certain range

I want to create 10,000 random numbers withing the range of 1 to 100.
How can I do with python?
I can use for loop and with each for loop I can use random.sample() or random.random() function.
Is there any other way, without using any for loop I can generate it with built-in function ?
For python 3.6:
import random
my_randoms = random.choices(range(1,100), k=10000)
print(my_randoms)
For older would still need to use loops:
my_randoms = [random.randrange(100) for x in range(10000)]

Categories

Resources