mercredi 24 février 2021

Python random number program

I'm trying to write a program that has a function that takes in three arguments and prints one integer.

Specific details:

  • 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 

I can make the program work if I write it like this:

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)

It also works if I write it like this:

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()

But when I try to switch it to this style, it gives no output. I'm confused by the whole sys thing. Please help me understand it.

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()

I also tried like this unsuccesfully:

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()



Aucun commentaire:

Enregistrer un commentaire