Skip to main content
"Vectorized" could be misleading here.
Source Link
Galen
  • 9.7k
  • 3
  • 27
  • 61

The question doesn't require a specific programming language, which is fine, but I noted that the OP's plot looks like the default style of . @jbowman has given a useful implementation. Here is a similar implementation in case the OP would like to continue their project in .

First we can define a vectorized function rsin.

import numpy as np

def rsin(n):
    results = np.zeros(n)
    for i in range(n):
        while True:
            u = np.random.uniform(0, 2*np.pi)
            u2 = np.random.uniform()
            if u2 <= np.abs(np.sin(u)):
                results[i] = u
                break
    return results

And then we can plot the results using .

import matplotlib.pyplot as plt

plt.hist(np.cos(rsin(10000)), bins=20)
plt.show()

Which produces the following plot:

enter image description here


@schotti's approach can be given (ignoring some of the style elements) in as follows.

import matplotlib.pyplot as plt

x = np.arccos(np.random.uniform(size=10**5))
y = np.cos(x)

fig, axes = plt.subplots(1,2)

axes[0].hist(x)
axes[1].hist(y)

for axis in axes:
    axis.set_xlabel('x')
    axis.set_ylabel('Frequency')
    axis.tick_params('y', labelrotation=90)

plt.show()

enter image description here

The question doesn't require a specific programming language, which is fine, but I noted that the OP's plot looks like the default style of . @jbowman has given a useful implementation. Here is a similar implementation in case the OP would like to continue their project in .

First we can define a vectorized function rsin.

import numpy as np

def rsin(n):
    results = np.zeros(n)
    for i in range(n):
        while True:
            u = np.random.uniform(0, 2*np.pi)
            u2 = np.random.uniform()
            if u2 <= np.abs(np.sin(u)):
                results[i] = u
                break
    return results

And then we can plot the results using .

import matplotlib.pyplot as plt

plt.hist(np.cos(rsin(10000)), bins=20)
plt.show()

Which produces the following plot:

enter image description here


@schotti's approach can be given (ignoring some of the style elements) in as follows.

import matplotlib.pyplot as plt

x = np.arccos(np.random.uniform(size=10**5))
y = np.cos(x)

fig, axes = plt.subplots(1,2)

axes[0].hist(x)
axes[1].hist(y)

for axis in axes:
    axis.set_xlabel('x')
    axis.set_ylabel('Frequency')
    axis.tick_params('y', labelrotation=90)

plt.show()

enter image description here

The question doesn't require a specific programming language, which is fine, but I noted that the OP's plot looks like the default style of . @jbowman has given a useful implementation. Here is a similar implementation in case the OP would like to continue their project in .

First we can define a function rsin.

import numpy as np

def rsin(n):
    results = np.zeros(n)
    for i in range(n):
        while True:
            u = np.random.uniform(0, 2*np.pi)
            u2 = np.random.uniform()
            if u2 <= np.abs(np.sin(u)):
                results[i] = u
                break
    return results

And then we can plot the results using .

import matplotlib.pyplot as plt

plt.hist(np.cos(rsin(10000)), bins=20)
plt.show()

Which produces the following plot:

enter image description here


@schotti's approach can be given (ignoring some of the style elements) in as follows.

import matplotlib.pyplot as plt

x = np.arccos(np.random.uniform(size=10**5))
y = np.cos(x)

fig, axes = plt.subplots(1,2)

axes[0].hist(x)
axes[1].hist(y)

for axis in axes:
    axis.set_xlabel('x')
    axis.set_ylabel('Frequency')
    axis.tick_params('y', labelrotation=90)

plt.show()

enter image description here

Added Python code for schotti's answer.
Source Link
Galen
  • 9.7k
  • 3
  • 27
  • 61

The question doesn't require a specific programming language, which is fine, but I noted that the OP's plot looks like the default style of . @jbowman has given a useful implementation. Here is a similar implementation in case the OP would like to continue their project in .

First we can define a vectorized function rsin.

import numpy as np

def rsin(n):
    results = np.zeros(n)
    for i in range(n):
        while True:
            u = np.random.uniform(0, 2*np.pi)
            u2 = np.random.uniform()
            if u2 <= np.abs(np.sin(u)):
                results[i] = u
                break
    return results

And then we can plot the results using .

import matplotlib.pyplot as plt

plt.hist(np.cos(rsin(10000)), bins=20)
plt.show()

Which produces the following plot:

enter image description here


@schotti's approach can be given (ignoring some of the style elements) in as follows.

import matplotlib.pyplot as plt

x = np.arccos(np.random.uniform(size=10**5))
y = np.cos(x)

fig, axes = plt.subplots(1,2)

axes[0].hist(x)
axes[1].hist(y)

for axis in axes:
    axis.set_xlabel('x')
    axis.set_ylabel('Frequency')
    axis.tick_params('y', labelrotation=90)

plt.show()

enter image description here

The question doesn't require a specific programming language, which is fine, but I noted that the OP's plot looks like the default style of . @jbowman has given a useful implementation. Here is a similar implementation in case the OP would like to continue their project in .

First we can define a vectorized function rsin.

import numpy as np

def rsin(n):
    results = np.zeros(n)
    for i in range(n):
        while True:
            u = np.random.uniform(0, 2*np.pi)
            u2 = np.random.uniform()
            if u2 <= np.abs(np.sin(u)):
                results[i] = u
                break
    return results

And then we can plot the results using .

import matplotlib.pyplot as plt

plt.hist(np.cos(rsin(10000)), bins=20)
plt.show()

Which produces the following plot:

enter image description here

The question doesn't require a specific programming language, which is fine, but I noted that the OP's plot looks like the default style of . @jbowman has given a useful implementation. Here is a similar implementation in case the OP would like to continue their project in .

First we can define a vectorized function rsin.

import numpy as np

def rsin(n):
    results = np.zeros(n)
    for i in range(n):
        while True:
            u = np.random.uniform(0, 2*np.pi)
            u2 = np.random.uniform()
            if u2 <= np.abs(np.sin(u)):
                results[i] = u
                break
    return results

And then we can plot the results using .

import matplotlib.pyplot as plt

plt.hist(np.cos(rsin(10000)), bins=20)
plt.show()

Which produces the following plot:

enter image description here


@schotti's approach can be given (ignoring some of the style elements) in as follows.

import matplotlib.pyplot as plt

x = np.arccos(np.random.uniform(size=10**5))
y = np.cos(x)

fig, axes = plt.subplots(1,2)

axes[0].hist(x)
axes[1].hist(y)

for axis in axes:
    axis.set_xlabel('x')
    axis.set_ylabel('Frequency')
    axis.tick_params('y', labelrotation=90)

plt.show()

enter image description here

Source Link
Galen
  • 9.7k
  • 3
  • 27
  • 61

The question doesn't require a specific programming language, which is fine, but I noted that the OP's plot looks like the default style of . @jbowman has given a useful implementation. Here is a similar implementation in case the OP would like to continue their project in .

First we can define a vectorized function rsin.

import numpy as np

def rsin(n):
    results = np.zeros(n)
    for i in range(n):
        while True:
            u = np.random.uniform(0, 2*np.pi)
            u2 = np.random.uniform()
            if u2 <= np.abs(np.sin(u)):
                results[i] = u
                break
    return results

And then we can plot the results using .

import matplotlib.pyplot as plt

plt.hist(np.cos(rsin(10000)), bins=20)
plt.show()

Which produces the following plot:

enter image description here