+ 11
How can I generate random even numbers in my program?
36 Answers
+ 44
import random
number_even = (random.randint(0,100000)) * 2
number_odd = (random.randint(0,100000)) * 2 +1
+ 9
import random as r
rn = r.choice(range(0, 101, 2))
print(rn)
+ 8
Generate a random integer that you times by two :P
( the range should be the half of your need ^^ )
+ 7
Simple logic:
1. Generate random number
2. Multiply by 2
+ 4
@ Mahdi Mohammadi:
There's no third 'step' parameter in the method 'randint()' ^^ ( and in Python 3.x 'print' isn't no more a statement, but a function, so it require parenthesis )
+ 4
Fill in the blanks to create a list of numbers multiplied by 10 in the range of 5 to 9.
a = [x*10
for
x
in
range(
5
, 9)]
+ 2
import random
random.randint.__doc__
"Return random integer in range [a,b], including both end point"
So, to generate one random number in a range:
import random
n=random.randint(100,200)
Next, to 'selectively' print many random numbers, you need to loop, and have a test condition to decide to print the generated number or not, at each loop ^^
+ 2
import random
lower_bound = -100
upper_bound = 101
step = 2
num = random.choice(range(lower_bound, upper_bound, step ))
print(num)
if you don't want negative number than change lower_bound to 0
if you want multiple numbers than create for loop and put last two lines in it
+ 2
import random
for i in range(10):
num = random.randint(0,40)
if num%2 == 0:
print(num)
else:
pass
+ 2
import random
def my_generator():
while True:
yield random.randrange(0,10000,2)
a=my_generator()
print(next(a))
#every time you run this code,it generates an even number in range 0-10000(you can easily change start and stop in rangerandom.randrange(start,stop,step),here step=2 to generate even numbers)
+ 1
import random
# random even integers from 0 to 100
# or any other range you choose to enter
print(random.choice(range(0,101,2)))
+ 1
If you're ever dealing with evens/odds, you need to know modulus. The formula you're working with would be x%2=0 where x is the number you're testing. To filter only evens your code would be
if (x % 2 = 0)
output x; (depending on what language you use your output command will be different)
+ 1
randrange method of random module will do it
for example:
import random
for i in range(20):
print(random.randrange(0,100,2))
+ 1
it's very simple
import random
number_even = (random.randint(0,100000)) * 2
number_odd = (random.randint(0,100000)) * 2 +1
use this code.
0
import random
num = random.randint (0,10000)
if(num%2 == 1)
num+=1
This would be the simplest way. You use modulus 2 to check if the number is odd, if it is, you add 1. Others are telling you to multiply by 2 and this isn't wrong, but you probably are also wanting to be able to keep your number within a range and have the numbers fluctuate more to seem more random. Give it a go :)
0
from random import randint
def rnd()
num = randint(0,100) # lower bound, upper bound
if num % 2==0:
return num
else:
return(rnd())
hopefully this works out well for you :)
0
The clearest and most understandable way is to test the output number using modulus within an if statement. Something like:
import randint from random
oddList = ()
num = randint(1,100)
if num%2 == 0:
oddList.append.(num)
should get you quite a long list of even numbers.
0
Import random from library and generate a random number.
Like --
import random as r
rnum=r.randint(range(1,10))
0
Thanks VIMALRAJ K
0
Fill in the blanks to create a list of numbers multiplied by 10 in the range of 5 to 9.
a = [x*10
for
x
in
range( 5, 9 ) ]