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









share|improve this question
























  • 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










  • @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















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









share|improve this question
























  • 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










  • @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













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • @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












  • 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
















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












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






share|improve this answer





















  • 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


















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.



Comparison of randi and randn



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.



Comparison of randi and 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)]})





share|improve this answer



















  • 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 using randn. But your advice is solid and I'll update when I get the chance. Thanks.
    – SecretAgentMan
    Nov 10 at 13:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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






share|improve this answer





















  • 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















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






share|improve this answer





















  • 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













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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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












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.



Comparison of randi and randn



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.



Comparison of randi and 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)]})





share|improve this answer



















  • 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 using randn. But your advice is solid and I'll update when I get the chance. Thanks.
    – SecretAgentMan
    Nov 10 at 13:06















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.



Comparison of randi and randn



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.



Comparison of randi and 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)]})





share|improve this answer



















  • 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 using randn. But your advice is solid and I'll update when I get the chance. Thanks.
    – SecretAgentMan
    Nov 10 at 13:06













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.



Comparison of randi and randn



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.



Comparison of randi and 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)]})





share|improve this answer














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.



Comparison of randi and randn



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.



Comparison of randi and 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)]})






share|improve this answer














share|improve this answer



share|improve this answer








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 using randn. But your advice is solid and I'll update when I get the chance. Thanks.
    – SecretAgentMan
    Nov 10 at 13:06














  • 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 using randn. 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff