4

I'm trying to address this question:

Generate 1,000 random samples of size 50 from population. Calculate the mean of each of these samples (so you should have 1,000 means) and put them in a list norm_samples_50.

My guess is I have to use the randn function, but I can't quite guess on how to form the syntax based on the question above. I've done the research and can't find an answer that fits.

user202729
  • 3,358
  • 3
  • 25
  • 36
Gitliong
  • 307
  • 1
  • 3
  • 7
  • Have you looked at the documentation to form the syntax? - it may help you – Ari K Apr 29 '18 at 22:48
  • What you've done so far? – Wakun Apr 29 '18 at 22:51
  • Yes - looked on documentation to form syntax. The problem is I can't figure out how the question and parameters I'm being given fits into the syntax. I've also search for YouTube vides to see how this might be constructed. I'm really going to Stack as a last resort. – Gitliong Apr 29 '18 at 22:54
  • The problem with questions like these is [we don't know what you're stuck on](http://idownvotedbecau.se/noresearch/). Do you have a Python compiler? Which version? Have you written any Python code? Do you know anything about programming at all? You do but ... what? You're getting an error? Post the error message. You're not getting an error but the code doesn't do what you expect? Post your code. What did you expect it to do? What does it do? – Dour High Arch Apr 30 '18 at 00:23
  • Thank you Dour High Arch for the lesson. As I get more familiar with the protocol of Stackoverflow - I'll keep your suggestions front and center. – Gitliong Apr 30 '18 at 17:00

2 Answers2

2

A very efficient solution using Numpy.

import numpy


sample_list = []

for i in range(50): # 50 times - we generate a 1000 of 0-1000random - 
    rand_list = numpy.random.randint(0,1000, 1000)
    # generates a list of 1000 elements with values 0-1000
    sample_list.append(sum(rand_list)/50) # sum all elements

Python one-liner

from numpy.random import randint


sample_list = [sum(randint(0,1000,1000))/50 for _ in range(50)]

Why use Numpy? It is very efficient and very accurate (decimal). This library is made just for these types of computations and numbers. Using random from the standard lib is fine but not nearly as speedy or reliable.

innicoder
  • 2,612
  • 3
  • 14
  • 29
1

Is this what you wanted?

import random

# Creating a population replace with your own: 
population = [random.randint(0, 1000) for x in range(1000)]

# Creating the list to store all the means of each sample: 
means = []

for x in range(1000):
    # Creating a random sample of the population with size 50: 
    sample = random.sample(population,50)
    # Getting the sum of values in the sample then dividing by 50: 
    mean = sum(sample)/50
    # Adding this mean to the list of means
    means.append(mean)
Zak Stucke
  • 432
  • 6
  • 18
  • 1
    Zak, it's a good answer but don't ask a question or show uncertainty even if you're not sure. You're here to provide the answer. Also another tip, always create a Summary. I used to do this a lot but this is a part of your answer that contains the whole answer, it should be composed of fewer words as there can be. Awesome answer tho!Thank you for your service and help! – innicoder Apr 30 '18 at 21:37