Skip to content

Commit

Permalink
Updated function header and REREADME, changed EXAMPLES script into a …
Browse files Browse the repository at this point in the history
…live script, and deleted unnecessary documentation.
  • Loading branch information
tamaskis committed Jun 8, 2021
1 parent ba4dab8 commit b2a8313
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 106 deletions.
Binary file removed DOCUMENTATION.pdf
Binary file not shown.
41 changes: 0 additions & 41 deletions EXAMPLES.m

This file was deleted.

Binary file added EXAMPLES.mlx
Binary file not shown.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# `rand2` [![View Random Number Generator in a Range (rand2) on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://www.mathworks.com/matlabcentral/fileexchange/85423-random-number-generator-in-a-range-rand2)

Generates a matrix of random numbers between specified lower and upper bounds.


## Syntax

`X = rand2(a,b)`\
`X = rand2(a,b,[],type)`\
`X = rand2(a,b,[m,n])`\
`X = rand2(a,b,[m,n],type)`


## Description

`X = rand2(a,b)` returns one random double-precision floating-point number `X` between `a` and `b`.

`X = rand2(a,b,[],typename)` returns one random number `X` of data type `type` between `a` and `b`. Options for `type` are `'int'` (integers), `'single'` (single-precision floating point numbers), or `'double'` (double-precision floating point numbers).

`X = rand2(a,b,[m,n])` returns an `m`-by-`n` matrix `X` of random double-precision floating-point numbers between `a` and `b`

`X = rand2(a,b,[m,n],typename)` returns an `m`-by-`n` matrix `X` of random numbers of data type `type`. Options for `type` are `'int'` (integers), `'single'` (single-precision floating point numbers), or `'double'` (double-precision floating point numbers).



## Examples

- See "EXAMPLES.mlx" or the "Examples" tab on the File Exchange page for examples.
27 changes: 0 additions & 27 deletions README.tex.md

This file was deleted.

75 changes: 37 additions & 38 deletions rand2.m
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
%==========================================================================
%
% rand2 Generates a matrix of random numbers between specified lower and
% upper bounds. Data type specification (integer, single, double) is
% available.
% upper bounds.
%
% X = rand2(a,b) returns one random double-precision floating-point
% number between a and b.
% X = rand2(a,b)
% X = rand2(a,b,[],type)
% X = rand2(a,b,[m,n])
% X = rand2(a,b,[m,n],type)
%
% X = rand2(a,b,[],typename) returns one random number of data type
% "typename" between a and b. The "typename" input can be either 'int',
% 'single', or 'double'.
% See also rand, randi
%
% X = rand2(a,b,[m,n]) returns an m-by-n matrix of random double-
% precision floating-point numbers between a and b.
% MATLAB Central File Exchange: https://www.mathworks.com/matlabcentral/fileexchange/85423-random-number-generator-in-a-range-rand2
% GitHub: https://github.com/tamaskis/rand2-MATLAB
%
% X = rand2(a,b,[m,n],typename) returns an m-by-n matrix of random
% numbers of data type "typename" between a and b. The "typename" input
% can be either 'int', 'single', or 'double'.
% See EXAMPLES.mlx for examples and "DOCUMENTATION.pdf" for additional
% documentation. Both of these files are included with the download.
%
% See also RAND, RANDI
% Copyright © 2021 Tamas Kis
% Last Update: 2021-06-08
%
% MATLAB Central File Exchange: https://www.mathworks.com/matlabcentral/fileexchange/85423-random-number-generator-in-a-range-rand2
% GitHub: https://github.com/tamaskis/rand2-MATLAB
%--------------------------------------------------------------------------
%
% See "DOCUMENTATION.pdf" for additional documentation and examples.
% Examples can also be found in EXAMPLES.m. Both of these files are
% included with the download.
% -------
% INPUTS:
% -------
% a - (1×1) lower bound
% b - (1×1) upper bound
% [m,n] - (1×2) (OPTIONAL) m = number of rows, n = number of columns
% --> defaults to [1,1]
% type - (char) (OPTIONAL) data type to return, three options:
% --> 'int' - integer
% --> 'single' - single-precision floating-point numbers
% --> 'double' - double-precision floating-point numbers
%
% Copyright (c) 2021 Tamas Kis
% Last Update: 2021-03-27



%% FUNCTION

% INPUT: a - lower bound
% b - upper bound
% [m,n] - m rows, n columns (OPTIONAL)
% typename - data type to return, three options (OPTIONAL):
% (1) 'int': integer
% (2) 'single': single-precision floating-point numbers
% (3) 'double': double-precision floating-point numbers
% OUTPUT: X - matrix of random numbers between a and b
function X = rand2(a,b,matrix_size,typename)
% --------
% OUTPUTS:
% --------
% X - (1×1 or m×n) matrix of random numbers between a and b
%
%==========================================================================
function X = rand2(a,b,matrix_size,type)

% sets default matrix size to 1-by-1 if matrix_size is not input (i.e.
% rand2 returns a single number by default)
Expand All @@ -52,23 +51,23 @@
% arguments (because in this case, we logically know that typename is
% not specified)
if nargin < 4
typename = 'double';
type = 'double';
end

% rounds lower and upper bounds if integer data type specified
if strcmp(typename,'int')
if strcmp(type,'int')
a = ceil(a);
b = floor(b);
end

% returns matrix of random integers between a and b
if strcmp(typename,'int')
if strcmp(type,'int')
X = randi([a,b],matrix_size);

% returns matrix of random floating-point numbers (either single or
% double precision, as specified by "typename") between a and b
else
X = a+(b-a)*rand(matrix_size(1),matrix_size(2),typename);
X = a+(b-a)*rand(matrix_size(1),matrix_size(2),type);

end

Expand Down

0 comments on commit b2a8313

Please sign in to comment.