Python随机数程序

2024-04-24 18:37:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一个程序,它有一个函数,接受三个参数并输出一个整数

具体细节:

  • Argument #1 should correspond to the size of a np.randint that has values from 0 to 10.
  • Argument #2 is an integer that you will multiply the randint by.
  • Argument #3 is a value you will index the result of the multiplication by.

Random seed set to 42.

You will print the integer that was indexed as ‘Your random value is x’ where x = the result of the indexing.

python3 reallyrandom.py 59 2 7  

Should generate the following:

Your random value is 12 

如果我这样写,我可以使程序工作:

import numpy as np
import pandas as pd
import random

def reallyrandom(arg1, arg2, arg3):
    
    int1=int(arg1)
    int2=int(arg2)
    int3=int(arg3)

    np.random.seed(42)

    x=np.random.randint(0,10, size=int1)
    y=x*int2
    z=y[int3]

    print("Your random value is {}".format(z))

reallyrandom(59,2,7)

如果我这样写,它也可以工作:

import numpy as np
import pandas as pd
import random

def reallyrandom():
    arg1=input()
    arg2=input()
    arg3=input()
    int1=int(arg1)
    int2=int(arg2)
    int3=int(arg3)

    np.random.seed(42)

    x=np.random.randint(0,10, size=int1)
    y=x*int2
    z=y[int3]

    print("Your random value is {}".format(z))

reallyrandom()

但当我尝试切换到这种风格时,它没有输出。我被整件事弄糊涂了。请帮助我理解它

import numpy as np
import pandas as pd
import random
import sys

def reallyrandom():
    arg1=sys.argv[1]
    arg2=sys.argv[2]
    arg3=sys.argv[3]
    int1=int(arg1)
    int2=int(arg2)
    int3=int(arg3)

    np.random.seed(42)

    x=np.random.randint(0,10, size=int1)
    y=x*int2
    z=y[int3]

    print("Your random value is {}".format(z))



reallyrandom()

我也这样试过,但没有成功:

import numpy as np
import pandas as pd
import random
import sys

def reallyrandom():

    int1=int(arg1)
    int2=int(arg2)
    int3=int(arg3)

    np.random.seed(42)

    x=np.random.randint(0,10, size=int1)
    y=x*int2
    z=y[int3]

    print("Your random value is {}".format(z))


if __name__ == '__main__':
    arg1=sys.argv[1]
    arg2=sys.argv[2]
    arg3=sys.argv[3]
    reallyrandom()

Tags: theimportisasnpsysrandomarg3