+ 11

How can I generate random even numbers in my program?

30th Jan 2017, 3:49 AM
Avijit Arya
Avijit Arya - avatar
37 Réponses
+ 44
import random number_even = (random.randint(0,100000)) * 2 number_odd = (random.randint(0,100000)) * 2 +1
2nd Feb 2017, 4:35 PM
mike98
mike98 - avatar
+ 9
import random as r rn = r.choice(range(0, 101, 2)) print(rn)
4th Feb 2017, 2:39 PM
Mark Foxx
Mark Foxx - avatar
+ 8
Generate a random integer that you times by two :P ( the range should be the half of your need ^^ )
30th Jan 2017, 4:48 AM
visph
visph - avatar
+ 7
Simple logic: 1. Generate random number 2. Multiply by 2
14th Feb 2017, 7:30 AM
Daniel Fernandes
Daniel Fernandes - avatar
+ 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 )
13th Feb 2017, 12:38 AM
visph
visph - avatar
+ 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)]
23rd Feb 2020, 3:59 AM
VIMALRAJ K
VIMALRAJ K - avatar
+ 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 ^^
30th Jan 2017, 5:02 AM
visph
visph - avatar
+ 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
10th Mar 2017, 4:05 AM
Gahan Saraiya
Gahan Saraiya - avatar
+ 2
import random for i in range(10): num = random.randint(0,40) if num%2 == 0: print(num) else: pass
12th Mar 2017, 7:20 PM
Zohaib Hassan
Zohaib Hassan - avatar
+ 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)
18th Aug 2020, 6:38 AM
arezou pakseresht
arezou pakseresht - avatar
+ 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)))
30th Jan 2017, 8:15 AM
richard
+ 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)
12th Feb 2017, 7:15 AM
Scarlet Red
Scarlet Red - avatar
+ 1
randrange method of random module will do it for example: import random for i in range(20): print(random.randrange(0,100,2))
9th Mar 2017, 10:38 PM
Alan Beveridge
Alan Beveridge - avatar
+ 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.
10th Mar 2017, 11:28 AM
Anurag Singhal
Anurag Singhal - avatar
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 :)
3rd Mar 2017, 5:05 AM
Joseph Amoroso
Joseph Amoroso - avatar
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 :)
9th Mar 2017, 10:38 PM
Callum Pritchard
Callum Pritchard - avatar
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.
11th Mar 2017, 12:09 PM
Carl Hansen
Carl Hansen - avatar
0
Import random from library and generate a random number. Like -- import random as r rnum=r.randint(range(1,10))
12th Mar 2017, 6:44 AM
Cybertron
Cybertron - avatar
0
Thanks VIMALRAJ K
29th Jun 2020, 6:00 PM
Faith Mundi
Faith Mundi - avatar
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 ) ]
6th Oct 2020, 1:22 PM
Shravani Jogu
Shravani Jogu - avatar