【问题标题】:random numbers even,odd,prime随机数偶数、奇数、素数
【发布时间】:2013-10-17 06:12:38
【问题描述】:
def FuncA():
    x = 0
    while x < 10:
        Hide = int(random()*1000) 
        if Hide > 99 and Hide < 1000 and Hide % 2 != 1:
            print Hide
            x = x + 1


def FuncB():
    Hide = 0
    for b in range(x):
        if Hide > 99 and Hide < 1000 and Hide % 2 == 0:
            print Hide

def FuncC():
    x = 0
    while x < 10:
        Hide = int(random()*1000) 
    if Hide > 99 and Hide < 1000 and Hide % 2 == 1:
        print Hide
        x = x + 1

def FuncD():
    x = 0
    while x < 10:
        Hide = int(random()*1000)
        if Hide > 99 and Hide < 1000 and Hide % 2 == 1:
            print Hide
            x = x + 1 

def FuncE():
    L = 0
    X = input("Please give me a number : ")
    for S in range(X):
        if X % (S+1) == 0:
            L = L + 1
            print S + 1
    if NumberOfTimesDivided == 2:
        print "The number is PRIME"
    else:
        print "The Number is NOT PRIME"

IM 尝试使用随机数来获取其中的 10 个数字,每个函数必须打印出 10 个不同的随机数。这需要帮助看看这是否正确或需要修复它。下面展示了每个函数想要做什么。

在 FuncA 中 # 打印 随机奇数 10 个在一行中

In FuncB #在一行中打印随机偶数10个

在 FuncC# 中打印随机奇数 3 位数字 10 在一行中

在 FuncD 中 # 打印 随机偶数 4 位数字 10 在一行中

在 FuncE # 中打印 随机素数 10 在一行中

我的问题是如何让他们为我制作的每个函数打印随机数。在上面,我希望他们执行一个命令,将随机数打印为一个奇数,另一个偶数,然后是奇数 3 位数字和 4 位数字,最后是素数。每个人都必须打印出10个不同的随机数。

【问题讨论】:

  • 正确格式化您的代码

标签: python random primes


【解决方案1】:
from random import choice

10 个随机奇数:

>>> print ', '.join(str(choice(range(1, 100, 2))) for _ in range(10))
45, 83, 57, 57, 85, 19, 49, 3, 5, 53

10 个随机偶数:

>>> print ', '.join(str(choice(range(2, 100, 2))) for _ in range(10))
44, 14, 4, 30, 82, 34, 38, 14, 34, 54

10 个随机奇数 3 位数:

>>> print ', '.join(str(choice(range(101, 1000, 2))) for _ in range(10))
485, 685, 555, 647, 513, 463, 729, 779, 229, 615

10 个随机偶数 4 位数字:

>>> print ', '.join(str(choice(range(1000, 10000, 2))) for _ in range(10))
7830, 3496, 3122, 5452, 3982, 7794, 8952, 2492, 4098, 1864

10 个随机素数

def pgen(maxnum): # Sieve of Eratosthenes generator
    yield 2
    np_f = {}
    for q in xrange(3, maxnum+1, 2):
        f = np_f.pop(q, None)
        if f:
            while f != np_f.setdefault(q+f, f):
                q += f
        else:
            yield q
            np = q*q
            if np < maxnum:
                np_f[np] = q+q

>>> print ', '.join(str(choice(list(pgen(1000)))) for _ in range(10))
151, 919, 59, 29, 373, 563, 991, 191, 607, 811

【讨论】:

    【解决方案2】:
    from random import randint as r
    from random import choice as c
    
    def FuncA ():
        print (' '.join (str (r (0, 499) * 2 + 1) for _ in range (10) ) )
    
    def FuncB ():
        print (' '.join (str (r (0, 499) * 2) for _ in range (10) ) )
    
    def FuncC ():
        print (' '.join (str (r (50, 499) * 2 + 1) for _ in range (10) ) )
    
    def FuncD ():
        print (' '.join (str (r (500, 4999) * 2) for _ in range (10) ) )
    
    def FuncE ():
        primes = [a for a, b in enumerate (all (c % i for i in range (2, c- 1) ) for c in range (1000) ) if b] [2:]
        print (' '.join (str (c (primes) ) for _ in range (10) ) )
    

    对于每个函数,输出都是 10 个满足每个标准的随机数,而不是您所说的 10 个不同的随机数。如果它们是 10 个不同的随机数,它们将不再是随机的。

    【讨论】:

    • Prime check 测试编号从 2 到 c-1 ... 认真吗? :)
    • “通过试除法确定素数……认真的吗?”本来是一个明智的问题。
    • 所以你知道你可以在 sqrt(c) 停止检查?
    【解决方案3】:

    从随机导入randint def FuncA():

       x = 0
    
       while x < 10:
           Hide = randint(1,1000)
    
           if Hide % 2 != 0:
              print Hide
    
              x = x + 1
    
    def FuncB():
    
       x = 0
    
       while x < 10:
           Hide = randint(1,1000) 
    
           if Hide % 2 == 0:
              print Hide
    
              x = x + 1
    
    def FuncC():
    
       x = 0
    
       while x < 10:
           Hide = randint(100,1000) 
    
           if Hide % 2 != 0:
              print Hide
    
              x = x + 1
    
    def FuncD():
    
       x = 0
    
       while x < 10:
           Hide = randint(1000,10000) 
    
           if Hide % 2 == 0:
              print Hide
    
              x = x + 1
    
    def FuncE():
    
       x = 0
    
       while x < 10:
           Hide = randint(1,1000) 
    
           for S in range(Hide):
             if Hide % (S+1) == 0:
                L = L + 1
    
    
    
           if L == 2:
               print Hide
               X + 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-18
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多