[Tutor] Data persistence problem
Dave Angel
davea at davea.name
Fri Jun 21 14:24:42 CEST 2013
On 06/21/2013 02:21 AM, Arijit Ukil wrote:
> I have following random number generation function
>
> def rand_int ():
> rand_num = int(math.ceil (random.random()*1000))
> return rand_num
>
> I like to make the value of rand_num (return of rand_int) static/
> unchanged after first call even if it is called multiple times. If x=
> rand_int () returns 45 at the first call, x should retain 45 even in
> multiple calls.
> Pls help.
>
You do realize that this will return the values 1 through 1000 each with
a probability of just under 0.1%, and return the value 0 extremely
rarely? I'd assume you really meant for it to exclude the value of 0
(or else make it a lot more likely).
def rand_int(precalc_value=random.randint(1,1000)):
return precalc_value
As long as you don't call it with a keyword of precalc_value, it'll
retain that initial value each time it's called.
And if you wanted the value of zero to also be equally likely (with a
probability of 1/1001)
def rand_int(precalc_value=random.randint(0,1000)):
return precalc_value
--
DaveA
More information about the Tutor
mailing list