Question

Write a program that uses the rand function to generate 500 integers, in the range [0,15)....

Write a program that uses the rand function to generate 500 integers, in the range [0,15).
Then calculate the mean and standard deviation.
0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Solution:

To generate the random number between (0,15) in python

  


import random #Import the random library to generate the random numbers

#Random Generator Function to get the random numbers between the range
def randomNumber_Generator():
 #declare array to store the random numbers    
 randomNumber=[]
 #using for loop generate 500 random numbers between 1to 15
 for i in range(1,500):
  val=random.randrange(0,15); #Using randrange module of random generate number
  randomNumber.append(val) #append value to the list
 return randomNumber; #return list of 500 random numbers
 

Calculate Mean:

Formula to calucale the Mean is SSumofthenumber/ sizeofthenumber

for eg let us take list number {1,2,3,4,5} & list size is 5

1+2+3+4+5/5=15/5

15/5=3

mean=5

In our case the Mean formula will be Sum of the each element in random Array/500

Program:

#Method to Calculate MEan value
def CalculateMean(value):
 # Initialize sum as Zero    
 sum=0;
 #Using for loop iterate random array and find sum of all values
 for j in range(0,len(value)):
    sum=sum+value[j];
 #Divide the sum by the length of array
 result=sum/len(value);
 #return result
 return result;

Standart Deviation:

Formula To Calculate StandardDeviation =n x - n-1 mean) i=1

step 1: Calculate Mean value for the list .Let us Consider above example 1+2+3+4+5=15/5=3

step2: Calculate square of Differente between elements and mean

  • 1-3=(-2)^2= 4
  • 2-3=(-1)^2=1
  • 3-3=(0)^2=0
  • 4-3=(1)^2=1
  • 5-3=(2)^2=4

step 3: Calculate sum of the square calculated in above steps

4+1+0+1+4=10

step 4 Calculate sumof the square divided by n-1

10/4=2.5

step5: Take Square root of the value V2.5 = 1.58

Similar Steps are followed in the program for the 500 random number

#method to calculatestandarddeviate
def CalculateStandardDeviation(randomNum,m):
 sum=0;
 # loop to calculate sum of (number-mean)^2
 for k in range(0,len(randomNum)):
    sum=sum+(randomNum[k]-m)*(randomNum[k]-m);
 #Divide sum of square by len of array -1 (here it is499)
 result=sum/(len(randomNum)-1);
#return take square root of result and return it
 return math.sqrt(result);

Whole Program:


import math #Import to call sqrt of maths function
import random #Import the random library to generate the random numbers

#Main Method
def main():
 #call the randomNumber_Generator method
 randomNumbers=randomNumber_Generator();
 #Call CalculateMeanMethod to get Mean
 mean=CalculateMean(randomNumbers)
 print("Mean Value:",mean);
 #Call CalculateStandardDeviation to standardDeviation
 standardDeviation=CalculateStandardDeviation(randomNumbers,mean);
 print("Standard Deviation:",standardDeviation); 
 
 #Random Generator Function to get the random numbers between the range
def randomNumber_Generator():
 #declare array to store the random numbers    
 randomNumber=[]
 #using for loop generate 500 random numbers between 1to 15
 for i in range(1,500):
  val=random.randrange(0,15); #Using randrange module of random generate number
  randomNumber.append(val) #append value to the list
 return randomNumber; #return list of 500 random numbers
 
#Method to Calculate MEan value
def CalculateMean(value):
 # Initialize sum as Zero    
 sum=0;
 #Using for loop iterate random array and find sum of all values
 for j in range(0,len(value)):
    sum=sum+value[j];
 #Divide the sum by the length of array
 result=sum/len(value);
 #return result
 return result;
 
 #Method to calculate standardDeviation
def CalculateStandardDeviation(randomNum,m):
 sum=0;
 #Loop to claculate sum of the random number -mean square
 for k in range(0,len(randomNum)):
    sum=sum+(randomNum[k]-m)*(randomNum[k]-m);
#Divide sum by the length of array -1 
 result=sum/(len(randomNum)-1);
 #return square root of the result
 return math.sqrt(result);
 
#Used to call Main Method

if __name__ == "__main__":
    main()

Ouput 1:

Mean Value: 6.8997995991983965

Standard Deviation: 4.414058860324523

Output 2:

Mean Value: 7.110220440881764

Standard Deviation: 4.318387387674145

Output 3:

Mean Value: 6.8937875751503

Standard Deviation: 4.372093582270911

Note:In the above program I didn't used the statistic library to calculate mean and standart deviation.If you want you can import statictics to calculate meann & SD.Kindly refer below code for that


import statistics #Import to call Mean and Standard deviation
import random #Import the random library to generate the random numbers

#Main Method
def main():
 #call the randomNumber_Generator method
 randomNumbers=randomNumber_Generator();
 #Call CalculateMeanMethod to get Mean
 mean=CalculateMean(randomNumbers)
 print("Mean Value:",mean);
 #Call CalculateStandardDeviation to standardDeviation
 standardDeviation=CalculateStandardDeviation(randomNumbers,mean);
 print("Standard Deviation:",standardDeviation); 
 
 #Random Generator Function to get the random numbers between the range
def randomNumber_Generator():
 #declare array to store the random numbers    
 randomNumber=[]
 #using for loop generate 500 random numbers between 1to 15
 for i in range(1,500):
  val=random.randrange(0,15); #Using randrange module of random generate number
  randomNumber.append(val) #append value to the list
 return randomNumber; #return list of 500 random numbers
 
#Method to Calculate MEan value
def CalculateMean(value):
 #Calculate the value using statistics mean method
 result=statistics.mean(value);
 #return result
 return result;
 
 #Method to calculate standardDeviation
def CalculateStandardDeviation(randomNum,m):
#Calculate it by passing array & mean value    
 result=statistics.stdev(randomNum, xbar = m);
 #return square root of the result
 return result;
 
#Used to call Main Method

if __name__ == "__main__":
    main()

Output:

Mean Value: 7.038076152304609

Standard Deviation: 4.387888715055942

Output:

Mean Value: 6.907815631262525

Standard Deviation: 4.224084587277824

Add a comment
Know the answer?
Add Answer to:
Write a program that uses the rand function to generate 500 integers, in the range [0,15)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT