Reputation: 13
I am trying to use random-integer
from srfi-27
inside a function but it always returns the same number everytime I call it.
(use srfi-27)
(define get-n
(random-integer 10))
(print get-n)
(print get-n)
(print (random-integer 10))
(print (random-integer 10))
(print (random-integer 10))
(print get-n)
(print get-n)
Output:
8
8
1
9
8
8
8
Can someone explain to me why this function always return to me the same number.
Additionally, how can I abstract the code for making the random number?
In the example I only ask for a number between 0 and 10 but in reality I want to do extra things to the number which is returned and I do not want to copy paste this code.
Upvotes: 1
Views: 878
Reputation: 780994
get-n
is not a function, it's just a variable that contains the result of a single call to random-integer
when you assigned it. If you want it to get a new random number each time, you have to define a function, and call it as a function.
(define get-n
(lambda() (random-integer 10)))
(print (get-n))
(print (get-n))
(print (get-n))
The above function definition syntax can be abbreviated as:
(define (get-n)
(random-integer 10))
Upvotes: 2