np.random.rand vs np.random.random











up vote
13
down vote

favorite
2












I find Python (and its ecosystem) to be full of strange conventions and inconsistencies and this is another example:



np.random.rand




Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).




np.random.random




Return random floats in the half-open interval [0.0, 1.0). Results are from the “continuous uniform” distribution over the stated interval.




??? What exactly is the difference there?










share|improve this question






















  • There is a difference between a "continuous uniform" distribution and a uniform distribution. This could help clarify it for you:docs.scipy.org/doc/numpy-1.13.0/reference/generated/…
    – user8502296
    Nov 10 '17 at 22:09








  • 1




    See stackoverflow.com/a/24542083/6328256
    – shash678
    Nov 10 '17 at 22:10










  • See en.wikipedia.org/wiki/Uniform_distribution_(continuous) and en.wikipedia.org/wiki/Discrete_uniform_distribution
    – Barmar
    Nov 10 '17 at 22:29










  • This is probably the same case here: stackoverflow.com/a/46634281/2285236
    – ayhan
    Nov 10 '17 at 22:43






  • 1




    @Caleb_McCreary I literally quoted from that page.. and no, there isn't a difference unless they had said "discrete" and "continuous", and discrete doesn't make sense between 0 and 1. Check the answer, the two functions are actually the same, seems like just a convention for matlab users and to confuse/annoy others
    – SpaceMonkey
    Nov 10 '17 at 22:55

















up vote
13
down vote

favorite
2












I find Python (and its ecosystem) to be full of strange conventions and inconsistencies and this is another example:



np.random.rand




Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).




np.random.random




Return random floats in the half-open interval [0.0, 1.0). Results are from the “continuous uniform” distribution over the stated interval.




??? What exactly is the difference there?










share|improve this question






















  • There is a difference between a "continuous uniform" distribution and a uniform distribution. This could help clarify it for you:docs.scipy.org/doc/numpy-1.13.0/reference/generated/…
    – user8502296
    Nov 10 '17 at 22:09








  • 1




    See stackoverflow.com/a/24542083/6328256
    – shash678
    Nov 10 '17 at 22:10










  • See en.wikipedia.org/wiki/Uniform_distribution_(continuous) and en.wikipedia.org/wiki/Discrete_uniform_distribution
    – Barmar
    Nov 10 '17 at 22:29










  • This is probably the same case here: stackoverflow.com/a/46634281/2285236
    – ayhan
    Nov 10 '17 at 22:43






  • 1




    @Caleb_McCreary I literally quoted from that page.. and no, there isn't a difference unless they had said "discrete" and "continuous", and discrete doesn't make sense between 0 and 1. Check the answer, the two functions are actually the same, seems like just a convention for matlab users and to confuse/annoy others
    – SpaceMonkey
    Nov 10 '17 at 22:55















up vote
13
down vote

favorite
2









up vote
13
down vote

favorite
2






2





I find Python (and its ecosystem) to be full of strange conventions and inconsistencies and this is another example:



np.random.rand




Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).




np.random.random




Return random floats in the half-open interval [0.0, 1.0). Results are from the “continuous uniform” distribution over the stated interval.




??? What exactly is the difference there?










share|improve this question













I find Python (and its ecosystem) to be full of strange conventions and inconsistencies and this is another example:



np.random.rand




Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).




np.random.random




Return random floats in the half-open interval [0.0, 1.0). Results are from the “continuous uniform” distribution over the stated interval.




??? What exactly is the difference there?







python numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 '17 at 22:05









SpaceMonkey

1,6041840




1,6041840












  • There is a difference between a "continuous uniform" distribution and a uniform distribution. This could help clarify it for you:docs.scipy.org/doc/numpy-1.13.0/reference/generated/…
    – user8502296
    Nov 10 '17 at 22:09








  • 1




    See stackoverflow.com/a/24542083/6328256
    – shash678
    Nov 10 '17 at 22:10










  • See en.wikipedia.org/wiki/Uniform_distribution_(continuous) and en.wikipedia.org/wiki/Discrete_uniform_distribution
    – Barmar
    Nov 10 '17 at 22:29










  • This is probably the same case here: stackoverflow.com/a/46634281/2285236
    – ayhan
    Nov 10 '17 at 22:43






  • 1




    @Caleb_McCreary I literally quoted from that page.. and no, there isn't a difference unless they had said "discrete" and "continuous", and discrete doesn't make sense between 0 and 1. Check the answer, the two functions are actually the same, seems like just a convention for matlab users and to confuse/annoy others
    – SpaceMonkey
    Nov 10 '17 at 22:55




















  • There is a difference between a "continuous uniform" distribution and a uniform distribution. This could help clarify it for you:docs.scipy.org/doc/numpy-1.13.0/reference/generated/…
    – user8502296
    Nov 10 '17 at 22:09








  • 1




    See stackoverflow.com/a/24542083/6328256
    – shash678
    Nov 10 '17 at 22:10










  • See en.wikipedia.org/wiki/Uniform_distribution_(continuous) and en.wikipedia.org/wiki/Discrete_uniform_distribution
    – Barmar
    Nov 10 '17 at 22:29










  • This is probably the same case here: stackoverflow.com/a/46634281/2285236
    – ayhan
    Nov 10 '17 at 22:43






  • 1




    @Caleb_McCreary I literally quoted from that page.. and no, there isn't a difference unless they had said "discrete" and "continuous", and discrete doesn't make sense between 0 and 1. Check the answer, the two functions are actually the same, seems like just a convention for matlab users and to confuse/annoy others
    – SpaceMonkey
    Nov 10 '17 at 22:55


















There is a difference between a "continuous uniform" distribution and a uniform distribution. This could help clarify it for you:docs.scipy.org/doc/numpy-1.13.0/reference/generated/…
– user8502296
Nov 10 '17 at 22:09






There is a difference between a "continuous uniform" distribution and a uniform distribution. This could help clarify it for you:docs.scipy.org/doc/numpy-1.13.0/reference/generated/…
– user8502296
Nov 10 '17 at 22:09






1




1




See stackoverflow.com/a/24542083/6328256
– shash678
Nov 10 '17 at 22:10




See stackoverflow.com/a/24542083/6328256
– shash678
Nov 10 '17 at 22:10












See en.wikipedia.org/wiki/Uniform_distribution_(continuous) and en.wikipedia.org/wiki/Discrete_uniform_distribution
– Barmar
Nov 10 '17 at 22:29




See en.wikipedia.org/wiki/Uniform_distribution_(continuous) and en.wikipedia.org/wiki/Discrete_uniform_distribution
– Barmar
Nov 10 '17 at 22:29












This is probably the same case here: stackoverflow.com/a/46634281/2285236
– ayhan
Nov 10 '17 at 22:43




This is probably the same case here: stackoverflow.com/a/46634281/2285236
– ayhan
Nov 10 '17 at 22:43




1




1




@Caleb_McCreary I literally quoted from that page.. and no, there isn't a difference unless they had said "discrete" and "continuous", and discrete doesn't make sense between 0 and 1. Check the answer, the two functions are actually the same, seems like just a convention for matlab users and to confuse/annoy others
– SpaceMonkey
Nov 10 '17 at 22:55






@Caleb_McCreary I literally quoted from that page.. and no, there isn't a difference unless they had said "discrete" and "continuous", and discrete doesn't make sense between 0 and 1. Check the answer, the two functions are actually the same, seems like just a convention for matlab users and to confuse/annoy others
– SpaceMonkey
Nov 10 '17 at 22:55














1 Answer
1






active

oldest

votes

















up vote
9
down vote



accepted










First note that numpy.random.random is actually an alias for numpy.random.random_sample. I'll use the latter in the following. (See this question and answer for more aliases.)



Both functions generate samples from the uniform distribution on [0, 1). The only difference is in how the arguments are handled. With numpy.random.rand, the length of each dimension of the output array is a separate argument. With numpy.random.random_sample, the shape argument is a single tuple.



For example, to create an array of samples with shape (3, 5), you can write



sample = np.random.rand(3, 5)


or



sample = np.random.random_sample((3, 5))


(Really, that's it.)






share|improve this answer























  • right, so there is actually no difference. I wonder why we have two functions with different names that do the same thing.. IMHO they should clean this up.
    – SpaceMonkey
    Nov 10 '17 at 22:50










  • There are historical reasons for these, probably related to making the transition to Python+Numpy easier for Matlab programmers (c.f. Matlab's rand). Cleaning it up might cause more trouble than it is worth, because there is likely a lot of existing code that uses the different versions of the functions.
    – Warren Weckesser
    Nov 10 '17 at 22:56








  • 4




    It does seem like Python's community values "quick hacks cuz it's easy for me" over consistency. The best example of this is the matplotlib's convention to use numbers like "112" to mean (1,1,2) when creating subplots.. anyway, thanks for your answer.
    – SpaceMonkey
    Nov 10 '17 at 22:58













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%2f47231852%2fnp-random-rand-vs-np-random-random%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
9
down vote



accepted










First note that numpy.random.random is actually an alias for numpy.random.random_sample. I'll use the latter in the following. (See this question and answer for more aliases.)



Both functions generate samples from the uniform distribution on [0, 1). The only difference is in how the arguments are handled. With numpy.random.rand, the length of each dimension of the output array is a separate argument. With numpy.random.random_sample, the shape argument is a single tuple.



For example, to create an array of samples with shape (3, 5), you can write



sample = np.random.rand(3, 5)


or



sample = np.random.random_sample((3, 5))


(Really, that's it.)






share|improve this answer























  • right, so there is actually no difference. I wonder why we have two functions with different names that do the same thing.. IMHO they should clean this up.
    – SpaceMonkey
    Nov 10 '17 at 22:50










  • There are historical reasons for these, probably related to making the transition to Python+Numpy easier for Matlab programmers (c.f. Matlab's rand). Cleaning it up might cause more trouble than it is worth, because there is likely a lot of existing code that uses the different versions of the functions.
    – Warren Weckesser
    Nov 10 '17 at 22:56








  • 4




    It does seem like Python's community values "quick hacks cuz it's easy for me" over consistency. The best example of this is the matplotlib's convention to use numbers like "112" to mean (1,1,2) when creating subplots.. anyway, thanks for your answer.
    – SpaceMonkey
    Nov 10 '17 at 22:58

















up vote
9
down vote



accepted










First note that numpy.random.random is actually an alias for numpy.random.random_sample. I'll use the latter in the following. (See this question and answer for more aliases.)



Both functions generate samples from the uniform distribution on [0, 1). The only difference is in how the arguments are handled. With numpy.random.rand, the length of each dimension of the output array is a separate argument. With numpy.random.random_sample, the shape argument is a single tuple.



For example, to create an array of samples with shape (3, 5), you can write



sample = np.random.rand(3, 5)


or



sample = np.random.random_sample((3, 5))


(Really, that's it.)






share|improve this answer























  • right, so there is actually no difference. I wonder why we have two functions with different names that do the same thing.. IMHO they should clean this up.
    – SpaceMonkey
    Nov 10 '17 at 22:50










  • There are historical reasons for these, probably related to making the transition to Python+Numpy easier for Matlab programmers (c.f. Matlab's rand). Cleaning it up might cause more trouble than it is worth, because there is likely a lot of existing code that uses the different versions of the functions.
    – Warren Weckesser
    Nov 10 '17 at 22:56








  • 4




    It does seem like Python's community values "quick hacks cuz it's easy for me" over consistency. The best example of this is the matplotlib's convention to use numbers like "112" to mean (1,1,2) when creating subplots.. anyway, thanks for your answer.
    – SpaceMonkey
    Nov 10 '17 at 22:58















up vote
9
down vote



accepted







up vote
9
down vote



accepted






First note that numpy.random.random is actually an alias for numpy.random.random_sample. I'll use the latter in the following. (See this question and answer for more aliases.)



Both functions generate samples from the uniform distribution on [0, 1). The only difference is in how the arguments are handled. With numpy.random.rand, the length of each dimension of the output array is a separate argument. With numpy.random.random_sample, the shape argument is a single tuple.



For example, to create an array of samples with shape (3, 5), you can write



sample = np.random.rand(3, 5)


or



sample = np.random.random_sample((3, 5))


(Really, that's it.)






share|improve this answer














First note that numpy.random.random is actually an alias for numpy.random.random_sample. I'll use the latter in the following. (See this question and answer for more aliases.)



Both functions generate samples from the uniform distribution on [0, 1). The only difference is in how the arguments are handled. With numpy.random.rand, the length of each dimension of the output array is a separate argument. With numpy.random.random_sample, the shape argument is a single tuple.



For example, to create an array of samples with shape (3, 5), you can write



sample = np.random.rand(3, 5)


or



sample = np.random.random_sample((3, 5))


(Really, that's it.)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 at 22:40

























answered Nov 10 '17 at 22:37









Warren Weckesser

66.9k792126




66.9k792126












  • right, so there is actually no difference. I wonder why we have two functions with different names that do the same thing.. IMHO they should clean this up.
    – SpaceMonkey
    Nov 10 '17 at 22:50










  • There are historical reasons for these, probably related to making the transition to Python+Numpy easier for Matlab programmers (c.f. Matlab's rand). Cleaning it up might cause more trouble than it is worth, because there is likely a lot of existing code that uses the different versions of the functions.
    – Warren Weckesser
    Nov 10 '17 at 22:56








  • 4




    It does seem like Python's community values "quick hacks cuz it's easy for me" over consistency. The best example of this is the matplotlib's convention to use numbers like "112" to mean (1,1,2) when creating subplots.. anyway, thanks for your answer.
    – SpaceMonkey
    Nov 10 '17 at 22:58




















  • right, so there is actually no difference. I wonder why we have two functions with different names that do the same thing.. IMHO they should clean this up.
    – SpaceMonkey
    Nov 10 '17 at 22:50










  • There are historical reasons for these, probably related to making the transition to Python+Numpy easier for Matlab programmers (c.f. Matlab's rand). Cleaning it up might cause more trouble than it is worth, because there is likely a lot of existing code that uses the different versions of the functions.
    – Warren Weckesser
    Nov 10 '17 at 22:56








  • 4




    It does seem like Python's community values "quick hacks cuz it's easy for me" over consistency. The best example of this is the matplotlib's convention to use numbers like "112" to mean (1,1,2) when creating subplots.. anyway, thanks for your answer.
    – SpaceMonkey
    Nov 10 '17 at 22:58


















right, so there is actually no difference. I wonder why we have two functions with different names that do the same thing.. IMHO they should clean this up.
– SpaceMonkey
Nov 10 '17 at 22:50




right, so there is actually no difference. I wonder why we have two functions with different names that do the same thing.. IMHO they should clean this up.
– SpaceMonkey
Nov 10 '17 at 22:50












There are historical reasons for these, probably related to making the transition to Python+Numpy easier for Matlab programmers (c.f. Matlab's rand). Cleaning it up might cause more trouble than it is worth, because there is likely a lot of existing code that uses the different versions of the functions.
– Warren Weckesser
Nov 10 '17 at 22:56






There are historical reasons for these, probably related to making the transition to Python+Numpy easier for Matlab programmers (c.f. Matlab's rand). Cleaning it up might cause more trouble than it is worth, because there is likely a lot of existing code that uses the different versions of the functions.
– Warren Weckesser
Nov 10 '17 at 22:56






4




4




It does seem like Python's community values "quick hacks cuz it's easy for me" over consistency. The best example of this is the matplotlib's convention to use numbers like "112" to mean (1,1,2) when creating subplots.. anyway, thanks for your answer.
– SpaceMonkey
Nov 10 '17 at 22:58






It does seem like Python's community values "quick hacks cuz it's easy for me" over consistency. The best example of this is the matplotlib's convention to use numbers like "112" to mean (1,1,2) when creating subplots.. anyway, thanks for your answer.
– SpaceMonkey
Nov 10 '17 at 22:58




















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%2f47231852%2fnp-random-rand-vs-np-random-random%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()