How do I create a single random number between two values?

I need to create a value for one of my variables in a loop which will run ten times. I need it to be between two set values, e.g. 1e3 and 9e3. Sorry for what is probably a very basic question but I am new to matlab.

 採用された回答

Wayne King
Wayne King 2012 年 3 月 22 日

5 投票

Does it have to be an integer, or any number between 1e3 and 9e3?
For the former:
Int = randi([1e3 9e3],1,1);
For the latter:
R = 1e3+8e3*rand(1,1);
The preceding line gives you a uniform random number between 1e3 and 9e3.

4 件のコメント

Marvin
Marvin 2012 年 3 月 22 日
Hi, for one value I need an integer and for another a number between 0.01 and 0.1.
The problem I am having is that the same number is being generated for every loop and I need it to change with each loop, is this possible?
Nathan Greco
Nathan Greco 2012 年 3 月 22 日
You can always change the random number generator seed. See: http://www.mathworks.com/help/techdoc/ref/randstream.html
Marvin
Marvin 2012 年 3 月 22 日
Thanks, I tried changing the seed and it didn't seem to work, the problem was that I was changing the seed at the beginning of the program, not inside the loop! Silly mistake. Thanks for all the help!
Aldin
Aldin 2012 年 3 月 22 日
Congratulations

サインインしてコメントする。

その他の回答 (3 件)

Image Analyst
Image Analyst 2012 年 3 月 22 日

3 投票

The help facility is always a good place to start. If you had searched the help for rand, you would have seen the very first example:
Example 1
Generate values from the uniform distribution on the interval [a, b]:
r = a + (b-a).*rand(100,1);

6 件のコメント

Marvin
Marvin 2012 年 3 月 22 日
Hi, I did check the tutorial but I'm unsure what the values (100,1) represent. As i said i'm a complete beginner so I apologise if my question has wasted your time.
Nathan Greco
Nathan Greco 2012 年 3 月 22 日
rand(100,1) means create a random matrix of size [100x1] of values on the open interval of (0,1).
Image Analyst
Image Analyst 2012 年 3 月 22 日
I agree that it might have been confusing since they didn't say what the 100 was right there in the example. You can send an email to support@mathworks.com if you find their documentation confusing. Perhaps they can change the wording to make it more clear. For example, tell them it should say "Generate 100 values from the uniform distribution on the interval [a, b]"
Andrew Sol
Andrew Sol 2023 年 3 月 13 日
And how to make it so that the minimum and maximum are included in the generated series of numbers?
Image Analyst
Image Analyst 2023 年 3 月 13 日
@Andrew Sol Since they will not be there (infinitesimally small chance) if you're just getting random numbers, you will have to add them in manually: Somthing like:
% Generate values from the uniform distribution on the interval [a, b]:
a = 5;
b = 10;
numElements = 7;
r = a + (b-a).*rand(numElements,1);
% Add in the max and min
r = [a; b; r];
% Scramble the array so they are not at the beginning
scrambledIndexes = randperm(numel(r));
r = r(scrambledIndexes)
r = 9×1
7.1316 10.0000 9.8263 6.0645 6.5554 7.7677 7.3234 5.0000 5.1012
Andrew Sol
Andrew Sol 2023 年 3 月 13 日
@Image Analyst Thank you very much for your answer! it's really not that complicated

サインインしてコメントする。

Aldin
Aldin 2012 年 3 月 22 日

0 投票

It's very helpful.
Nisha Bharti
Nisha Bharti 2022 年 1 月 12 日

0 投票

To generate a random number between [a,b]:
r = a + (b-a)*rand();
To generate N numbers:
r = a + (b-a)*rand(1,N);

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2012 年 3 月 22 日

コメント済み:

2023 年 3 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by