4

For example:

round(7*rand(1,5))

Generates 5 numbers between 1 and 7 Is there a way to generate 5 random numbers between 5 and 7? Or an abstraction of that?

Amro
  • 123,847
  • 25
  • 243
  • 454
Ben Fossen
  • 1,331
  • 6
  • 21
  • 33

2 Answers2

10

More generally:

minInt = 5;
maxInt = 7;
numInts = 10;

r = randi([minInt, maxInt],[1,numInts])

r =

 6     7     7     7     6     5     5     5     7     5
MatlabDoug
  • 5,704
  • 1
  • 24
  • 36
5

First, if you are wanting to generate random integer values, it's better to use the function RANDI. Then it's just a matter of shifting and scaling the random numbers accordingly. The following should give you random integers between 5 and 7 inclusive:

nums = randi(3,[1 5])+4;

EDIT: As Amro's comment and Doug's answer point out, there is a more straightforward solution whereby you can specify the range directly as the first argument to RANDI:

nums = randi([5 7],[1 5]);
Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359