Generating larger random integers - MATLAB
up vote
-2
down vote
favorite
Trying to assign appropriate value to 'x' that would result in random integers between 1 to 60. Any suggestions? I did 'randn' but am getting small numbers over and over. Here's the code so far:
function s = Q11sub1(x)
x =
if x <= 30
s = "small";
elseif x > 30 & x <= 50
s = "medium";
else
s = "high";
end
end
matlab random integer
add a comment |
up vote
-2
down vote
favorite
Trying to assign appropriate value to 'x' that would result in random integers between 1 to 60. Any suggestions? I did 'randn' but am getting small numbers over and over. Here's the code so far:
function s = Q11sub1(x)
x =
if x <= 30
s = "small";
elseif x > 30 & x <= 50
s = "medium";
else
s = "high";
end
end
matlab random integer
If you're having a problem with a particular program show us that program please. Show us how you are usingrandn
and we can help you figure out why it is not doing what you expect.
– Eric Lippert
Nov 10 at 0:52
I note that yourx > 30
check is redundant. If It were<= 30
then you already executed the consequence of theif
.
– Eric Lippert
Nov 10 at 0:53
@EricLippert,randn
generates continuous random variates from the standard Normal distribution, e.g. N(mean = 0, std = 1). That function is entirely wrong for this purpose.
– SecretAgentMan
Nov 10 at 3:42
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
Trying to assign appropriate value to 'x' that would result in random integers between 1 to 60. Any suggestions? I did 'randn' but am getting small numbers over and over. Here's the code so far:
function s = Q11sub1(x)
x =
if x <= 30
s = "small";
elseif x > 30 & x <= 50
s = "medium";
else
s = "high";
end
end
matlab random integer
Trying to assign appropriate value to 'x' that would result in random integers between 1 to 60. Any suggestions? I did 'randn' but am getting small numbers over and over. Here's the code so far:
function s = Q11sub1(x)
x =
if x <= 30
s = "small";
elseif x > 30 & x <= 50
s = "medium";
else
s = "high";
end
end
matlab random integer
matlab random integer
edited Nov 10 at 9:52
SecretAgentMan
508113
508113
asked Nov 10 at 0:48
Jacqueline Lu
1
1
If you're having a problem with a particular program show us that program please. Show us how you are usingrandn
and we can help you figure out why it is not doing what you expect.
– Eric Lippert
Nov 10 at 0:52
I note that yourx > 30
check is redundant. If It were<= 30
then you already executed the consequence of theif
.
– Eric Lippert
Nov 10 at 0:53
@EricLippert,randn
generates continuous random variates from the standard Normal distribution, e.g. N(mean = 0, std = 1). That function is entirely wrong for this purpose.
– SecretAgentMan
Nov 10 at 3:42
add a comment |
If you're having a problem with a particular program show us that program please. Show us how you are usingrandn
and we can help you figure out why it is not doing what you expect.
– Eric Lippert
Nov 10 at 0:52
I note that yourx > 30
check is redundant. If It were<= 30
then you already executed the consequence of theif
.
– Eric Lippert
Nov 10 at 0:53
@EricLippert,randn
generates continuous random variates from the standard Normal distribution, e.g. N(mean = 0, std = 1). That function is entirely wrong for this purpose.
– SecretAgentMan
Nov 10 at 3:42
If you're having a problem with a particular program show us that program please. Show us how you are using
randn
and we can help you figure out why it is not doing what you expect.– Eric Lippert
Nov 10 at 0:52
If you're having a problem with a particular program show us that program please. Show us how you are using
randn
and we can help you figure out why it is not doing what you expect.– Eric Lippert
Nov 10 at 0:52
I note that your
x > 30
check is redundant. If It were <= 30
then you already executed the consequence of the if
.– Eric Lippert
Nov 10 at 0:53
I note that your
x > 30
check is redundant. If It were <= 30
then you already executed the consequence of the if
.– Eric Lippert
Nov 10 at 0:53
@EricLippert,
randn
generates continuous random variates from the standard Normal distribution, e.g. N(mean = 0, std = 1). That function is entirely wrong for this purpose.– SecretAgentMan
Nov 10 at 3:42
@EricLippert,
randn
generates continuous random variates from the standard Normal distribution, e.g. N(mean = 0, std = 1). That function is entirely wrong for this purpose.– SecretAgentMan
Nov 10 at 3:42
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
Use randi
:
randi(60)
This will give you a pseudorandom integer between 1 to 60.
Reference: https://www.mathworks.com/help/matlab/ref/randi.html
I've upvoted but wanted to add they will be uniformly distributed across the integers between those bounds (known as the discrete uniform distribution).
– SecretAgentMan
Nov 10 at 3:27
add a comment |
up vote
2
down vote
The problem is randn
generates random numbers that follow a standard Normal distribution, e.g. Normal(mu = 0, std = 1).
As @Banghua Zhao points out, you want the randi
function and I'll add they will be uniformly distributed across the integers (inclusively) between those integer bounds (known as the discrete uniform distribution).
The code X = randi([a b],N,M)
will generate a NxM matrix of integers uniformly distributed on the interval [a,b] inclusively. A call randi(Imax)
defaults the lower bound to 1.
See the difference below.
N = 500; % Number of samples
a = 1; % Lower integer bound
b = 60; % Upper integer bound
X = randi([a b],N,1); % Random integers between [a,b]
Y = randn(N,1);
figure, hold on, box on
histogram(X)
histogram(Y)
legend('randi[1,60]','randn','Location','southeast')
xlabel('Result')
ylabel('Observed Frequency')
title({'randi([a b],N,1) vs randn(N,1)';'N = 500'})
EDIT: At @Max's suggestion, I've added 60*randn
.
W = 60*randn(N,1);
figure, hold on, box on
hx = histogram(X,'Normalization','pdf')
hw = histogram(W,'Normalization','pdf')
legend('randi[1,60]','60*randn','Location','southeast')
xlabel('Result')
ylabel('Observed Estimated Density')
title({'randi([a b],N,1) vs 60*randn(N,1)';['N = ' num2str(N)]})
1
Maybe, the difference between randi and randn would become even more clear, if you multiply randn by 60 and then plot again
– Max
Nov 10 at 9:32
@Max, thanks for the suggestion. I thought of that but I wasn't clear how the OP was usingrandn
. But your advice is solid and I'll update when I get the chance. Thanks.
– SecretAgentMan
Nov 10 at 13:06
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Use randi
:
randi(60)
This will give you a pseudorandom integer between 1 to 60.
Reference: https://www.mathworks.com/help/matlab/ref/randi.html
I've upvoted but wanted to add they will be uniformly distributed across the integers between those bounds (known as the discrete uniform distribution).
– SecretAgentMan
Nov 10 at 3:27
add a comment |
up vote
2
down vote
Use randi
:
randi(60)
This will give you a pseudorandom integer between 1 to 60.
Reference: https://www.mathworks.com/help/matlab/ref/randi.html
I've upvoted but wanted to add they will be uniformly distributed across the integers between those bounds (known as the discrete uniform distribution).
– SecretAgentMan
Nov 10 at 3:27
add a comment |
up vote
2
down vote
up vote
2
down vote
Use randi
:
randi(60)
This will give you a pseudorandom integer between 1 to 60.
Reference: https://www.mathworks.com/help/matlab/ref/randi.html
Use randi
:
randi(60)
This will give you a pseudorandom integer between 1 to 60.
Reference: https://www.mathworks.com/help/matlab/ref/randi.html
answered Nov 10 at 1:05
Banghua Zhao
989617
989617
I've upvoted but wanted to add they will be uniformly distributed across the integers between those bounds (known as the discrete uniform distribution).
– SecretAgentMan
Nov 10 at 3:27
add a comment |
I've upvoted but wanted to add they will be uniformly distributed across the integers between those bounds (known as the discrete uniform distribution).
– SecretAgentMan
Nov 10 at 3:27
I've upvoted but wanted to add they will be uniformly distributed across the integers between those bounds (known as the discrete uniform distribution).
– SecretAgentMan
Nov 10 at 3:27
I've upvoted but wanted to add they will be uniformly distributed across the integers between those bounds (known as the discrete uniform distribution).
– SecretAgentMan
Nov 10 at 3:27
add a comment |
up vote
2
down vote
The problem is randn
generates random numbers that follow a standard Normal distribution, e.g. Normal(mu = 0, std = 1).
As @Banghua Zhao points out, you want the randi
function and I'll add they will be uniformly distributed across the integers (inclusively) between those integer bounds (known as the discrete uniform distribution).
The code X = randi([a b],N,M)
will generate a NxM matrix of integers uniformly distributed on the interval [a,b] inclusively. A call randi(Imax)
defaults the lower bound to 1.
See the difference below.
N = 500; % Number of samples
a = 1; % Lower integer bound
b = 60; % Upper integer bound
X = randi([a b],N,1); % Random integers between [a,b]
Y = randn(N,1);
figure, hold on, box on
histogram(X)
histogram(Y)
legend('randi[1,60]','randn','Location','southeast')
xlabel('Result')
ylabel('Observed Frequency')
title({'randi([a b],N,1) vs randn(N,1)';'N = 500'})
EDIT: At @Max's suggestion, I've added 60*randn
.
W = 60*randn(N,1);
figure, hold on, box on
hx = histogram(X,'Normalization','pdf')
hw = histogram(W,'Normalization','pdf')
legend('randi[1,60]','60*randn','Location','southeast')
xlabel('Result')
ylabel('Observed Estimated Density')
title({'randi([a b],N,1) vs 60*randn(N,1)';['N = ' num2str(N)]})
1
Maybe, the difference between randi and randn would become even more clear, if you multiply randn by 60 and then plot again
– Max
Nov 10 at 9:32
@Max, thanks for the suggestion. I thought of that but I wasn't clear how the OP was usingrandn
. But your advice is solid and I'll update when I get the chance. Thanks.
– SecretAgentMan
Nov 10 at 13:06
add a comment |
up vote
2
down vote
The problem is randn
generates random numbers that follow a standard Normal distribution, e.g. Normal(mu = 0, std = 1).
As @Banghua Zhao points out, you want the randi
function and I'll add they will be uniformly distributed across the integers (inclusively) between those integer bounds (known as the discrete uniform distribution).
The code X = randi([a b],N,M)
will generate a NxM matrix of integers uniformly distributed on the interval [a,b] inclusively. A call randi(Imax)
defaults the lower bound to 1.
See the difference below.
N = 500; % Number of samples
a = 1; % Lower integer bound
b = 60; % Upper integer bound
X = randi([a b],N,1); % Random integers between [a,b]
Y = randn(N,1);
figure, hold on, box on
histogram(X)
histogram(Y)
legend('randi[1,60]','randn','Location','southeast')
xlabel('Result')
ylabel('Observed Frequency')
title({'randi([a b],N,1) vs randn(N,1)';'N = 500'})
EDIT: At @Max's suggestion, I've added 60*randn
.
W = 60*randn(N,1);
figure, hold on, box on
hx = histogram(X,'Normalization','pdf')
hw = histogram(W,'Normalization','pdf')
legend('randi[1,60]','60*randn','Location','southeast')
xlabel('Result')
ylabel('Observed Estimated Density')
title({'randi([a b],N,1) vs 60*randn(N,1)';['N = ' num2str(N)]})
1
Maybe, the difference between randi and randn would become even more clear, if you multiply randn by 60 and then plot again
– Max
Nov 10 at 9:32
@Max, thanks for the suggestion. I thought of that but I wasn't clear how the OP was usingrandn
. But your advice is solid and I'll update when I get the chance. Thanks.
– SecretAgentMan
Nov 10 at 13:06
add a comment |
up vote
2
down vote
up vote
2
down vote
The problem is randn
generates random numbers that follow a standard Normal distribution, e.g. Normal(mu = 0, std = 1).
As @Banghua Zhao points out, you want the randi
function and I'll add they will be uniformly distributed across the integers (inclusively) between those integer bounds (known as the discrete uniform distribution).
The code X = randi([a b],N,M)
will generate a NxM matrix of integers uniformly distributed on the interval [a,b] inclusively. A call randi(Imax)
defaults the lower bound to 1.
See the difference below.
N = 500; % Number of samples
a = 1; % Lower integer bound
b = 60; % Upper integer bound
X = randi([a b],N,1); % Random integers between [a,b]
Y = randn(N,1);
figure, hold on, box on
histogram(X)
histogram(Y)
legend('randi[1,60]','randn','Location','southeast')
xlabel('Result')
ylabel('Observed Frequency')
title({'randi([a b],N,1) vs randn(N,1)';'N = 500'})
EDIT: At @Max's suggestion, I've added 60*randn
.
W = 60*randn(N,1);
figure, hold on, box on
hx = histogram(X,'Normalization','pdf')
hw = histogram(W,'Normalization','pdf')
legend('randi[1,60]','60*randn','Location','southeast')
xlabel('Result')
ylabel('Observed Estimated Density')
title({'randi([a b],N,1) vs 60*randn(N,1)';['N = ' num2str(N)]})
The problem is randn
generates random numbers that follow a standard Normal distribution, e.g. Normal(mu = 0, std = 1).
As @Banghua Zhao points out, you want the randi
function and I'll add they will be uniformly distributed across the integers (inclusively) between those integer bounds (known as the discrete uniform distribution).
The code X = randi([a b],N,M)
will generate a NxM matrix of integers uniformly distributed on the interval [a,b] inclusively. A call randi(Imax)
defaults the lower bound to 1.
See the difference below.
N = 500; % Number of samples
a = 1; % Lower integer bound
b = 60; % Upper integer bound
X = randi([a b],N,1); % Random integers between [a,b]
Y = randn(N,1);
figure, hold on, box on
histogram(X)
histogram(Y)
legend('randi[1,60]','randn','Location','southeast')
xlabel('Result')
ylabel('Observed Frequency')
title({'randi([a b],N,1) vs randn(N,1)';'N = 500'})
EDIT: At @Max's suggestion, I've added 60*randn
.
W = 60*randn(N,1);
figure, hold on, box on
hx = histogram(X,'Normalization','pdf')
hw = histogram(W,'Normalization','pdf')
legend('randi[1,60]','60*randn','Location','southeast')
xlabel('Result')
ylabel('Observed Estimated Density')
title({'randi([a b],N,1) vs 60*randn(N,1)';['N = ' num2str(N)]})
edited Nov 10 at 13:46
answered Nov 10 at 3:25
SecretAgentMan
508113
508113
1
Maybe, the difference between randi and randn would become even more clear, if you multiply randn by 60 and then plot again
– Max
Nov 10 at 9:32
@Max, thanks for the suggestion. I thought of that but I wasn't clear how the OP was usingrandn
. But your advice is solid and I'll update when I get the chance. Thanks.
– SecretAgentMan
Nov 10 at 13:06
add a comment |
1
Maybe, the difference between randi and randn would become even more clear, if you multiply randn by 60 and then plot again
– Max
Nov 10 at 9:32
@Max, thanks for the suggestion. I thought of that but I wasn't clear how the OP was usingrandn
. But your advice is solid and I'll update when I get the chance. Thanks.
– SecretAgentMan
Nov 10 at 13:06
1
1
Maybe, the difference between randi and randn would become even more clear, if you multiply randn by 60 and then plot again
– Max
Nov 10 at 9:32
Maybe, the difference between randi and randn would become even more clear, if you multiply randn by 60 and then plot again
– Max
Nov 10 at 9:32
@Max, thanks for the suggestion. I thought of that but I wasn't clear how the OP was using
randn
. But your advice is solid and I'll update when I get the chance. Thanks.– SecretAgentMan
Nov 10 at 13:06
@Max, thanks for the suggestion. I thought of that but I wasn't clear how the OP was using
randn
. But your advice is solid and I'll update when I get the chance. Thanks.– SecretAgentMan
Nov 10 at 13:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235034%2fgenerating-larger-random-integers-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you're having a problem with a particular program show us that program please. Show us how you are using
randn
and we can help you figure out why it is not doing what you expect.– Eric Lippert
Nov 10 at 0:52
I note that your
x > 30
check is redundant. If It were<= 30
then you already executed the consequence of theif
.– Eric Lippert
Nov 10 at 0:53
@EricLippert,
randn
generates continuous random variates from the standard Normal distribution, e.g. N(mean = 0, std = 1). That function is entirely wrong for this purpose.– SecretAgentMan
Nov 10 at 3:42