[Tutor] Need help to generate random number without library functions.
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Sep 7 19:11:41 EDT 2020
On 07/09/2020 21:45, ROHAN RASKAR wrote:
> This is my code :
>
>
> import numpy as np
> import math
Those lines say you are going to be using features
of the np and math libraries. However there is no
mention of the np module anywhere in the code
that follows. So either you don't need to import
it or you are missing some references.
> 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)
This function does a lot of work to no purpose.
The variables only last for the time that the function is
executing. Every time you call the function they get created
afresh. You never return a value nor do you print anything.
So all the work happens internally and then gets thrown away.
Also several of your variables are not used after being
initialized at all. (ie. n, value, fd, r, d, e)
Those are just a waste of space and computing resources.
You declare x to be a dictionary but then try to subtract
u, a number, from it. That makes no sense, either to me or
to Python.
I suspect you really want to be able to pass arguments
to this function and get something back.
But its not clear what.
As it is, none of this matters much since you never
call the function, so none of it ever gets executed.
> 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)
Here we have a loop that uses lots of variables that
have not been created yet - x, e,d,fd,value, values
Note that this code cannot see the variables you defined
within your function. In fact they don't even exist
outside the function.
I suspect you wanted to define your variables outside
the function as global variables (a bad habit but we
won't get into that here!). But that still doesn't change
the fact that you never call the function so it appears
to be redundant!
Assuming thats the case we still have problems.
- You call fd.get() but fd is a list and list don't have
a get() method - that's a dictionary thing.
- You use value as if it were a collection of some
kind(you call len()) but then you go on to assign a numerical
result to it so next time round the loop len() will return 1.
That is almost certainly not what you want!
- You append stuff to values, but never refer to values
later. So thats just wasted effort.
>> I want to generate 1000 random numbers which are normally
>> distributed with mean 0.6 and 0.2 standard deviations.
First write a function that takes the target mean and SD values as
arguments and returns a random number. Once you have that generating
10000 of them is trivial. But get a function working for one first:
def random(mean, SD):
number = # your code here
return number
>> am getting some errors in code. Could you guide me? Thank you so much.
Always post errors in full, they contain a lot of useful information.
However, in this case there are so many problems it probably
doesn't make much difference!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list