0

I am trying to study for my final in a Python class, and I'm stumped on one of the questions.

I am supposed to write a program that generates random integers from 1 to 100 such that they are "distributed as a sine function," as in the histogram of results will follow a sine curve. Any help that could point me in the right direction would be greatly appreciated.

Link to question

Matthew Gunn
  • 4,451
  • 1
  • 12
  • 30
Matt
  • 23
  • 5
  • What do you mean "distributed as a sine function"? You mean the density is a sine function? Over what interval? Do you add a constant so that the density is not negative? you need to clarify... – Matthew Gunn Dec 18 '15 at 22:02
  • 2
    Re: "the values of the sine function should be between 1 and 100" ... I'm pretty sure the values of the sine function are between -1 and 1. – Chad S. Dec 18 '15 at 22:05
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your code and accurately describe the problem. StackOverflow is not a coding or tutorial service. – Prune Dec 18 '15 at 22:08
  • The range of the sine function is [-1, 1]. I'm tempted to say this as, at least technically, an ill posed question. Perhaps if you post the full question or an image of the full question or a link to the full question, perhaps someone could say something. Right now, it doesn't make much sense. – Matthew Gunn Dec 18 '15 at 22:08
  • I apologize for not being very clear, I'm a first time poster and I don't completely understand the question myself. I've edited the post with a link to the question. – Matt Dec 18 '15 at 22:15

2 Answers2

1

IMHO, this is an ambiguous, poorly worded question. I'll take a stab at what they're trying to ask and how I would answer it.

My interpretation of the question

Generate 5000 random integers from the set {1, 2, ..., 100} such that the probability mass function p(x) is proportional to (sin(x)+1).

Sketch of how I would answer the question

  1. Construct the probability mass function (essentially a vector)
  2. Use the vector (which defines the pmf) to generate draws from a categorical distribution (or multinomial distribution with n = 1).

Constructing this probability mass function is easy. I will use a vector to represent the function (i.e. f(1) is the first element of the vector, f(2) is the 2nd element of the vector etc...)

  • Construct a vector x = 1,2,...,100
  • Construct a new vector p_temp = sin(x) + 1
  • Construct a new vector pm = p_temp / sum(p_temp) so that the probabilities sum to 1

Then pm(1) would be probability of drawing 1, pm(2) would be probability of drawing 2 etc... Once you have the pm vector, there are several ways to generate the random integers. Mathematically, you're drawing from a categorical distribution (could search for that). Python has functions for this, though it would also be easy to code your own.

Community
  • 1
  • 1
Matthew Gunn
  • 4,451
  • 1
  • 12
  • 30
0

Try here https://www.cs.utexas.edu/~mitra/csSpring2015/cs313/lectures/math.html. Here is the example code

import matplotlib.pyplot as pl
import numpy as np

x = np.linspace(1, 10)

def f(x):
    return np.sin(x) + np.random.normal(scale=0.1, size=len(x))

pl.plot(x, f(x))
Darren
  • 145
  • 1
  • 1
  • 13