Question

In: Computer Science

We will now simulate numpy.random(), which produces floating-point values in the range [0..1). To do this,...

We will now simulate numpy.random(), which produces floating-point values in the range [0..1).

To do this, it is simply necessary to convert integers in the range [0..?)[0..m) returned by my_random_int() to floating point numbers in the range [0..1).[0..1).

Hint: Easily done by division!

# my_random returns a random floating-point number within [0, 1)

def my_random():
# your code here
return 0   # just to get it to compile


# Test it!
my_seed(0)
for x in range(0,10):
print(my_random())

CODE GIVEN:

a = 914334
m = 2**22 - 3

#a = 3 # You will use these in part (b)
#m = 7

def hash(x):
return (a * x) % m

# Test it!
X = [231,45,123,87,133,123]
for x in X:
print(hash(x))

next_value = 1 # just to create the variable

# seed next_value with the hash of (n+1)
def my_seed(n):
global next_value
next_value = hash(n+1) # so values do not start with seed and are not 0   

# my_random_int() returns a random number generated by the hash function, in the range [0..(m-1)].

def my_random_int():
global next_value
next_value = hash(next_value)
return next_value
  
# Test it

my_seed(0)

for x in range(10):
print(my_random_int())

Solutions

Expert Solution

python code

import numpy as np

# my_random returns a random floating-point number within (0, 1)

def my_random():
# return random float numbers
return np.random.uniform(0, 1)   


# Test it!
#my_seed(0)
for x in range(0,10):
print(my_random())#print numbers

code &output:

//i have given the needed code alone, please do comments if you need any clarification or modification.


Related Solutions

Q1: In the addition of floating-point numbers, how do we adjust the representation of numbers with...
Q1: In the addition of floating-point numbers, how do we adjust the representation of numbers with different exponents? Q2: Answer the following questions: What binary operation can be used to set bits? What bit pattern should the mask have? What binary operation can be used to unset bits? What bit pattern should the mask have? What binary operation can be used to flip bits? What bit pattern should the mask have?
Write a program that asks the user to input a set of floating-point values. When the...
Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs.5 pts Your code with comments A screenshot of the execution Test Case:       Enter float: 1.0...
1. Calculate the step values for the following ADC bit sizes over a range of 0...
1. Calculate the step values for the following ADC bit sizes over a range of 0 to 3.3v. a.4 Bit b.8 Bit c.10 Bit d.12 Bit 2.Calculate the step size for 4, 8, 10, and 12 Bit analog to digital conversation of the temperature sensor with a range of 90 to 100 degrees fahrenheit. 3.Identify the minimum ADC bit size that is needed to convert the temperature with one tenth of a degree accuracy.
Choose a point at random from the unit square [0, 1] × [0, 1]. We also...
Choose a point at random from the unit square [0, 1] × [0, 1]. We also choose the second random point, independent of the first, uniformly on the line segment between (0, 0) and (1, 0). The random variable A is the area of a triangle with its corners at (0, 0) and the two selected points. Find the probability density function (pdf) of A.
1. Tolerance is a measure of collinearity among IVs, where possible values range from 0–1. T...
1. Tolerance is a measure of collinearity among IVs, where possible values range from 0–1. T F 2. The variance inflation factor (VIF) for a given predictor “indicates whether there exists a strong linear association between it and all remaining predictors” (Stevens, 2001). T F 3. In standard multiple regression, the IV that has the highest correlation with the DV is entered into the analysis first. T F 4. Sequential multiple regression is also sometimes referred to as statistical multiple...
We consider the experiment of picking a point at random from the interval (0, 1). The...
We consider the experiment of picking a point at random from the interval (0, 1). The sample space for this experiment is the interval (0, 1). We consider three sequences of events in this sample space En = ( 1 n , 1) Fn = (1 − 1 n , 1), Gn = ( 1 2 − 1 2n , 1 − 1 2n ), n positive integer. (a) Is (En) ∞ n=1 increasing, decreasing, both, or neither? (b) Is...
Using the range 0 ≤ x ≤ 1 and 0 ≤ t ≤ 1 make an...
Using the range 0 ≤ x ≤ 1 and 0 ≤ t ≤ 1 make an animated plot of: 1.) y1(x,t)=cos(4πt)sin(πx) 2.) y2(x,t)=cos(8πt)sin(2πx) using matlab
We will simulate a dice game in which 2 dice are thrown. If the roll is...
We will simulate a dice game in which 2 dice are thrown. If the roll is 7 or 11, you win. If the roll is 2, 3, or 12, you lose If the roll is any other value, it establishes a point. If with a point established, that point is rolled again before a 7, you win. If, with a point established, a 7 is rolled before the point is rolled again you lose. Build your algorithm incrementally. First write...
DO NOT DO THE GRAPH OR THE DOMAIN OR RANGE PART. THOSE DO NOT COUNT. WE...
DO NOT DO THE GRAPH OR THE DOMAIN OR RANGE PART. THOSE DO NOT COUNT. WE ARE NOT DOING THEM IN MATH 1003.Find the factors that are common in the numerator and the denominator. Then find the intercepts and asymptotes. (If an answer does not exist, enter DNE. Enter your asymptotes as a comma-separated list of equations if necessary.) r(x) = x2 + 4x − 5 x2 + x − 2 x-intercept     (x, y) =    y-intercept     (x, y) =...
**C++** Output each floating-point value with two digits after the decimal point, which can be achieved...
**C++** Output each floating-point value with two digits after the decimal point, which can be achieved by executing cout << fixed << setprecision(2); once before all other cout statements. (1) Prompt the user to input a wall's height and width. Calculate and output the wall's area. (2 pts) Note: This zyLab outputs a newline after each user-input prompt. For convenience in the examples below, the user's input value is shown on the next line, but such values don't actually appear...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT