[Tutor] Need help to generate random number without library functions.

boB Stepp robertvstepp at gmail.com
Mon Sep 7 18:28:25 EDT 2020


On Tue, Sep 08, 2020 at 02:15:34AM +0530, ROHAN RASKAR wrote:
>This is my code :
>
>
> import numpy as np
>import math
>def normal():
>    n=1000 ##number of random numders
>    u=0.6 ##mean
>    sd=0.2 ##standard deviation
>    value = []
>    x={}
>    fd=[]
>    pi=math.pi
>    r = 2.0*sd**2
>    a = x - u
>    d = math.sqrt(2.0*pi)
>    e = math.exp(-a*a)
>
>while len(value) < n:
>     if 0 < x < 10:
>       value = (e/r)/(d*0.2)
>     fd[int(value)] = fd.get(int(value), 0) + 1
>     values.append(value)
>     print(value)
>
>
>On Tue, Sep 8, 2020 at 1:09 AM ROHAN RASKAR <rohanraskar24 at gmail.com> wrote:
>
>> Dear Sir/Ma'am,
>>                           I'm a student and beginner for learning python
>> language. I want to generate 1000 random numbers which are normally
>> distributed with mean 0.6 and 0.2 standard deviations. I tried a lot but I
>> am getting some errors in code. Could you guide me?  Thank you so much.

OK, you have code posted above.  What about the errors?

Anyway, I have yet to have a need for numpy.  So I only have very
superficial knowledge of it.  But you import numpy as "np".  So
whenever you use a numpy method or function you need to prefix that method
or function name with "np."  I don't see you doing this anywhere, so are
you using numpy or not?

You have this "normal()" function, but where do you call it?  In it you
have:

x={} which to my eye assigns an empty dictionary to x.  But you never add
any entries to it.  However you have "a = x - u" where it appears you are
trying to subtract a float from an empty dictionary.  That does not make
much sense!

fd=[] which normally would be assigning an empty list to fd.  You don't use
fd inside normal(), but do reference an fd in your while loop.  Since fd is
not global, the while loop cannot see it as it has only local scope to
normal().

Finally your normal() function does not return anything nor do I see
anywhere in your code where it is called.

Now your while loop again seems to be referencing identifiers used in your
normal() function, but these variables are local to normal(); they are not
global variables!  So all of these variables are undefined inside the while
loop.

So are you wanting a function at all?  Or are you wanting the while loop to
be part of your function?  If there is going to be one (or more) functions,
where are these going to be called?  And what values will be returned for
further processing?

Are some of your identifiers meant to reference numpy methods/functions?
If yes then you need to prefix them with "np."  For instance, if "fd" is
really a numpy entity then you need to aceess it as "np.fd"

As I say I am not familiar with numpy, but these are the things that
immediately come to my mind.  I imagine your tracebacks pointed out some of
these issues!

Good luck!  Hopefully someone with numpy experience can drill down to more
specific things.

-- 
Wishing you only the best,

boB Stepp


More information about the Tutor mailing list