Creating a list of random elements using recursion
up vote
3
down vote
favorite
I was asked to define a recursive function that takes in two parameters:
n
valmax
and returns a list of
n
numbers picked randomly from the interval[0 , valmax]
`
import random
def random_list(n, valmax, lst = ):
"""
parameters : n of type int;
valmax of type int;
returns : a list of n numbers picked randomly from the interval
[0, valmax]
"""
if len(lst) == n:
return lst
return [random.randint(0, valmax)] + random_list(n, valmax)
print(random_list(10,100))`
However, I'm getting an
RecursionError
How can I fix my code so that it returns a list with n
random numbers in the interval [0, valmax]
?
python recursion random
add a comment |
up vote
3
down vote
favorite
I was asked to define a recursive function that takes in two parameters:
n
valmax
and returns a list of
n
numbers picked randomly from the interval[0 , valmax]
`
import random
def random_list(n, valmax, lst = ):
"""
parameters : n of type int;
valmax of type int;
returns : a list of n numbers picked randomly from the interval
[0, valmax]
"""
if len(lst) == n:
return lst
return [random.randint(0, valmax)] + random_list(n, valmax)
print(random_list(10,100))`
However, I'm getting an
RecursionError
How can I fix my code so that it returns a list with n
random numbers in the interval [0, valmax]
?
python recursion random
you are trying to slice lst in your recursive step, but the list is empty at first. you may want to rethink if you'd want to slice it in the first place.
– Paritosh Singh
Nov 11 at 19:17
Do you want to allow repetitions?
– schwobaseggl
Nov 11 at 19:20
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I was asked to define a recursive function that takes in two parameters:
n
valmax
and returns a list of
n
numbers picked randomly from the interval[0 , valmax]
`
import random
def random_list(n, valmax, lst = ):
"""
parameters : n of type int;
valmax of type int;
returns : a list of n numbers picked randomly from the interval
[0, valmax]
"""
if len(lst) == n:
return lst
return [random.randint(0, valmax)] + random_list(n, valmax)
print(random_list(10,100))`
However, I'm getting an
RecursionError
How can I fix my code so that it returns a list with n
random numbers in the interval [0, valmax]
?
python recursion random
I was asked to define a recursive function that takes in two parameters:
n
valmax
and returns a list of
n
numbers picked randomly from the interval[0 , valmax]
`
import random
def random_list(n, valmax, lst = ):
"""
parameters : n of type int;
valmax of type int;
returns : a list of n numbers picked randomly from the interval
[0, valmax]
"""
if len(lst) == n:
return lst
return [random.randint(0, valmax)] + random_list(n, valmax)
print(random_list(10,100))`
However, I'm getting an
RecursionError
How can I fix my code so that it returns a list with n
random numbers in the interval [0, valmax]
?
python recursion random
python recursion random
edited Nov 11 at 19:19
asked Nov 11 at 19:13
user10158754
you are trying to slice lst in your recursive step, but the list is empty at first. you may want to rethink if you'd want to slice it in the first place.
– Paritosh Singh
Nov 11 at 19:17
Do you want to allow repetitions?
– schwobaseggl
Nov 11 at 19:20
add a comment |
you are trying to slice lst in your recursive step, but the list is empty at first. you may want to rethink if you'd want to slice it in the first place.
– Paritosh Singh
Nov 11 at 19:17
Do you want to allow repetitions?
– schwobaseggl
Nov 11 at 19:20
you are trying to slice lst in your recursive step, but the list is empty at first. you may want to rethink if you'd want to slice it in the first place.
– Paritosh Singh
Nov 11 at 19:17
you are trying to slice lst in your recursive step, but the list is empty at first. you may want to rethink if you'd want to slice it in the first place.
– Paritosh Singh
Nov 11 at 19:17
Do you want to allow repetitions?
– schwobaseggl
Nov 11 at 19:20
Do you want to allow repetitions?
– schwobaseggl
Nov 11 at 19:20
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
Your logic is wrong. You need each function call to return n
random integers, so you do not need to pass it in a list.
Each function generates a single random number in the range [0, valmax]
and concatenates it to the random list of integers which is of length one less (n-1
) which it gets from calling itself recursively.
The base case is when n == 1
, in which case we return an empty list.
import random
def random_list(n, valmax):
if n == 0:
return
return [random.randint(0, valmax)] + random_list(n-1, valmax)
and a test:
random_list(10, 20)
#[20, 9, 4, 7, 3, 4, 3, 18, 19, 9]
add a comment |
up vote
2
down vote
Instead of keeping a default parameter (which can also cause unexpected behavior on consecutive calls), use yield
for a cleaner solution. Also, simply use random.randint(0, valmax)
to generate a single random integer between 0
and valmax
:
import random
def random_list(n, valmax):
if n:
yield random.randint(0, valmax)
yield from random_list(n-1, valmax)
print(list(random_list(10, 10))) #create a list of length ten with random values between 0 and 10, inclusive.
Output:
[4, 6, 9, 1, 10, 2, 2, 8, 2, 10]
add a comment |
up vote
1
down vote
You could write a generic build_list
function -
import random
def identity (x):
return x
def build_list (size, proc = identity):
if size == 0:
return
else:
return build_list (size - 1, proc) + [ proc (size - 1) ]
print (build_list (5))
# [ 0, 1, 2, 3, 4 ]
print (build_list (5, lambda _: random.randint (0, 10)))
# [ 4, 7, 7, 3, 6 ]
random_list
could be a specialization of build_list
-
def random_list (size, valmax):
return build_list (size, lambda _: random.randint (0, valmax))
print (random_list (5, 10))
# [ 1, 7, 4, 7, 0 ]
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Your logic is wrong. You need each function call to return n
random integers, so you do not need to pass it in a list.
Each function generates a single random number in the range [0, valmax]
and concatenates it to the random list of integers which is of length one less (n-1
) which it gets from calling itself recursively.
The base case is when n == 1
, in which case we return an empty list.
import random
def random_list(n, valmax):
if n == 0:
return
return [random.randint(0, valmax)] + random_list(n-1, valmax)
and a test:
random_list(10, 20)
#[20, 9, 4, 7, 3, 4, 3, 18, 19, 9]
add a comment |
up vote
3
down vote
accepted
Your logic is wrong. You need each function call to return n
random integers, so you do not need to pass it in a list.
Each function generates a single random number in the range [0, valmax]
and concatenates it to the random list of integers which is of length one less (n-1
) which it gets from calling itself recursively.
The base case is when n == 1
, in which case we return an empty list.
import random
def random_list(n, valmax):
if n == 0:
return
return [random.randint(0, valmax)] + random_list(n-1, valmax)
and a test:
random_list(10, 20)
#[20, 9, 4, 7, 3, 4, 3, 18, 19, 9]
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Your logic is wrong. You need each function call to return n
random integers, so you do not need to pass it in a list.
Each function generates a single random number in the range [0, valmax]
and concatenates it to the random list of integers which is of length one less (n-1
) which it gets from calling itself recursively.
The base case is when n == 1
, in which case we return an empty list.
import random
def random_list(n, valmax):
if n == 0:
return
return [random.randint(0, valmax)] + random_list(n-1, valmax)
and a test:
random_list(10, 20)
#[20, 9, 4, 7, 3, 4, 3, 18, 19, 9]
Your logic is wrong. You need each function call to return n
random integers, so you do not need to pass it in a list.
Each function generates a single random number in the range [0, valmax]
and concatenates it to the random list of integers which is of length one less (n-1
) which it gets from calling itself recursively.
The base case is when n == 1
, in which case we return an empty list.
import random
def random_list(n, valmax):
if n == 0:
return
return [random.randint(0, valmax)] + random_list(n-1, valmax)
and a test:
random_list(10, 20)
#[20, 9, 4, 7, 3, 4, 3, 18, 19, 9]
answered Nov 11 at 19:19
Joe Iddon
14.7k31538
14.7k31538
add a comment |
add a comment |
up vote
2
down vote
Instead of keeping a default parameter (which can also cause unexpected behavior on consecutive calls), use yield
for a cleaner solution. Also, simply use random.randint(0, valmax)
to generate a single random integer between 0
and valmax
:
import random
def random_list(n, valmax):
if n:
yield random.randint(0, valmax)
yield from random_list(n-1, valmax)
print(list(random_list(10, 10))) #create a list of length ten with random values between 0 and 10, inclusive.
Output:
[4, 6, 9, 1, 10, 2, 2, 8, 2, 10]
add a comment |
up vote
2
down vote
Instead of keeping a default parameter (which can also cause unexpected behavior on consecutive calls), use yield
for a cleaner solution. Also, simply use random.randint(0, valmax)
to generate a single random integer between 0
and valmax
:
import random
def random_list(n, valmax):
if n:
yield random.randint(0, valmax)
yield from random_list(n-1, valmax)
print(list(random_list(10, 10))) #create a list of length ten with random values between 0 and 10, inclusive.
Output:
[4, 6, 9, 1, 10, 2, 2, 8, 2, 10]
add a comment |
up vote
2
down vote
up vote
2
down vote
Instead of keeping a default parameter (which can also cause unexpected behavior on consecutive calls), use yield
for a cleaner solution. Also, simply use random.randint(0, valmax)
to generate a single random integer between 0
and valmax
:
import random
def random_list(n, valmax):
if n:
yield random.randint(0, valmax)
yield from random_list(n-1, valmax)
print(list(random_list(10, 10))) #create a list of length ten with random values between 0 and 10, inclusive.
Output:
[4, 6, 9, 1, 10, 2, 2, 8, 2, 10]
Instead of keeping a default parameter (which can also cause unexpected behavior on consecutive calls), use yield
for a cleaner solution. Also, simply use random.randint(0, valmax)
to generate a single random integer between 0
and valmax
:
import random
def random_list(n, valmax):
if n:
yield random.randint(0, valmax)
yield from random_list(n-1, valmax)
print(list(random_list(10, 10))) #create a list of length ten with random values between 0 and 10, inclusive.
Output:
[4, 6, 9, 1, 10, 2, 2, 8, 2, 10]
answered Nov 11 at 19:21
Ajax1234
39.4k42552
39.4k42552
add a comment |
add a comment |
up vote
1
down vote
You could write a generic build_list
function -
import random
def identity (x):
return x
def build_list (size, proc = identity):
if size == 0:
return
else:
return build_list (size - 1, proc) + [ proc (size - 1) ]
print (build_list (5))
# [ 0, 1, 2, 3, 4 ]
print (build_list (5, lambda _: random.randint (0, 10)))
# [ 4, 7, 7, 3, 6 ]
random_list
could be a specialization of build_list
-
def random_list (size, valmax):
return build_list (size, lambda _: random.randint (0, valmax))
print (random_list (5, 10))
# [ 1, 7, 4, 7, 0 ]
add a comment |
up vote
1
down vote
You could write a generic build_list
function -
import random
def identity (x):
return x
def build_list (size, proc = identity):
if size == 0:
return
else:
return build_list (size - 1, proc) + [ proc (size - 1) ]
print (build_list (5))
# [ 0, 1, 2, 3, 4 ]
print (build_list (5, lambda _: random.randint (0, 10)))
# [ 4, 7, 7, 3, 6 ]
random_list
could be a specialization of build_list
-
def random_list (size, valmax):
return build_list (size, lambda _: random.randint (0, valmax))
print (random_list (5, 10))
# [ 1, 7, 4, 7, 0 ]
add a comment |
up vote
1
down vote
up vote
1
down vote
You could write a generic build_list
function -
import random
def identity (x):
return x
def build_list (size, proc = identity):
if size == 0:
return
else:
return build_list (size - 1, proc) + [ proc (size - 1) ]
print (build_list (5))
# [ 0, 1, 2, 3, 4 ]
print (build_list (5, lambda _: random.randint (0, 10)))
# [ 4, 7, 7, 3, 6 ]
random_list
could be a specialization of build_list
-
def random_list (size, valmax):
return build_list (size, lambda _: random.randint (0, valmax))
print (random_list (5, 10))
# [ 1, 7, 4, 7, 0 ]
You could write a generic build_list
function -
import random
def identity (x):
return x
def build_list (size, proc = identity):
if size == 0:
return
else:
return build_list (size - 1, proc) + [ proc (size - 1) ]
print (build_list (5))
# [ 0, 1, 2, 3, 4 ]
print (build_list (5, lambda _: random.randint (0, 10)))
# [ 4, 7, 7, 3, 6 ]
random_list
could be a specialization of build_list
-
def random_list (size, valmax):
return build_list (size, lambda _: random.randint (0, valmax))
print (random_list (5, 10))
# [ 1, 7, 4, 7, 0 ]
answered Nov 11 at 20:14
user633183
67.2k21133173
67.2k21133173
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252243%2fcreating-a-list-of-random-elements-using-recursion%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you are trying to slice lst in your recursive step, but the list is empty at first. you may want to rethink if you'd want to slice it in the first place.
– Paritosh Singh
Nov 11 at 19:17
Do you want to allow repetitions?
– schwobaseggl
Nov 11 at 19:20