How to specify , n ,the number of random integers

3 views (last 30 days)
Write a Matlab function r = random(x,y) which takes as input two integers x and y, and generates and returns a random integer that is equally likely to take any value between x and y?
Then, write a program use random() to generate 100,000 random numbers between 1 and 10. Store each random number in an vector.
Heres what I got so far:
function r = random(x,y) r = floor(x + y.*rand(n,1)) ??? end
program: n = 100000; x = 1; y = 10; r = random(x,y);

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 16 Aug 2011
1
function r = random(x,y)
r = round(x + (y-x).*rand); end
OR
random = @(x,y)randi([x, y])
2
function r = randomn(x,y,n)
r = round(x + (y-x).*rand(n,1));
OR
randomn = @(x,y,n)randi([x y],n,1)
more
randomn = @(x,y,n)arrayfun(@(i1)random(x , y),(1:n)')

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!