0

I'm trying to create a loop that takes an input value, and creates that many random numbers between 0 and 10, and then puts these values throuh a formula in a loop and saves these values into an array. The following is what I've done:

def f(x):
    x**2

def Sample(npts):
    sample = []

    randomlist = random.sample(range(0.0, 10.0), npts)


    for i in range(randomlist):
        y  = f(randomlist)
        sample.append(y)

    print(sample)

now here I am getting an error saying that the sample range is being exceeded. This occurrs when I set npts = 100, can someone explain where I am going wrong? Also, I fear that I may have called the wrong value later down the line. So to explain, I am trying to get 100 random values (npts, in this case) between 0 and 10, and these values be the x values that are put through f(x), (in this case the values being multiplied by 2) and then have these new calculated values saved to the array sample each time and then printed at the end.

Hope that makes what I am trying to do clear, so please let me know how to fix the error!

2
  • 1
    Even if you got the random number generation working, your function f() doesn't return anything, so sample is just going to be a list of Nones. Commented Nov 18, 2021 at 4:38
  • 1
    I get an error on range(0.0, 10.0). What does your code really look like? Commented Nov 18, 2021 at 4:52

2 Answers 2

1

from random.sample documentation: https://docs.python.org/3/library/random.html#random.sample

Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

You are choosing 100 items out of 10 items without replacement, thus there is an error.

If you want to sample with replacement, use random.choices: https://docs.python.org/3/library/random.html#random.choices

Return a k sized list of elements chosen from the population with replacement. If the population is empty, raises IndexError.

Sign up to request clarification or add additional context in comments.

Comments

0

Study this.

import random


def f(x):
    return x**2


def Sample(npts):
    sample = []

    # Generate random numbers from 0 to 10.
    randomlist = []
    for _ in range(npts):  # just counting from 0 to npts
        r = random.randint(0, 10) # returns integer value randomly in the range [0, 10]
        randomlist.append(r)  # save to a list

    # Save result of function f
    for v in randomlist: # get each item in the list
        y = f(v)
        sample.append(y)

    return sample

# start
npts = 100
result = Sample(npts)
print(f'result length: {len(result)}')
print(f'result: {result}')

Output

result length: 100
result: [4, 16, 1, 49, 100, 16, 4, 81, 81, ...

2 Comments

Thanks this has been helpful, I get now whats been done, I was probably making it too complicated for myself, but trying to do it all in one go
Sorry I revised the code so that the random number generator will generate integer random numbers in the range [0 and 10].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.