Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41135 > unrolled thread

How can i create a random array of floats from 0 to 5 in python

Started byNorah Jones <nh.jones01@gmail.com>
First post2013-03-12 17:11 +0000
Last post2013-03-12 10:47 -0700
Articles 8 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  How can i create a random array of floats from 0 to 5 in python Norah Jones <nh.jones01@gmail.com> - 2013-03-12 17:11 +0000
    Re: How can i create a random array of floats from 0 to 5 in python Maarten <maarten.sneep@knmi.nl> - 2013-03-12 10:47 -0700
      Re: How can i create a random array of floats from 0 to 5 in python llanitedave <llanitedave@veawb.coop> - 2013-03-12 13:21 -0700
        Re: How can i create a random array of floats from 0 to 5 in python Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-03-12 21:59 +0000
          Re: How can i create a random array of floats from 0 to 5 in python llanitedave <llanitedave@veawb.coop> - 2013-03-12 22:35 -0700
          Re: How can i create a random array of floats from 0 to 5 in python llanitedave <llanitedave@veawb.coop> - 2013-03-12 22:35 -0700
      Re: How can i create a random array of floats from 0 to 5 in python llanitedave <llanitedave@veawb.coop> - 2013-03-12 13:21 -0700
    Re: How can i create a random array of floats from 0 to 5 in python Maarten <maarten.sneep@knmi.nl> - 2013-03-12 10:47 -0700

#41135 — How can i create a random array of floats from 0 to 5 in python

FromNorah Jones <nh.jones01@gmail.com>
Date2013-03-12 17:11 +0000
SubjectHow can i create a random array of floats from 0 to 5 in python
Message-ID<mailman.3237.1363108626.2939.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?

[toc] | [next] | [standalone]


#41140

FromMaarten <maarten.sneep@knmi.nl>
Date2013-03-12 10:47 -0700
Message-ID<0aa38f5a-0e5e-43cd-b6ba-69af6f37e94e@googlegroups.com>
In reply to#41135
On Tuesday, March 12, 2013 6:11:10 PM UTC+1, Norah Jones wrote:
> I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?

Use numpy

import numpy as np
np.random.uniform(0, 5, 100)

# note that the values are from the interval [0, 5)

Maarten

[toc] | [prev] | [next] | [standalone]


#41149

Fromllanitedave <llanitedave@veawb.coop>
Date2013-03-12 13:21 -0700
Message-ID<bcb46e43-51c7-40ba-bd4a-3787ce9f8ceb@googlegroups.com>
In reply to#41140
On Tuesday, March 12, 2013 10:47:25 AM UTC-7, Maarten wrote:
> On Tuesday, March 12, 2013 6:11:10 PM UTC+1, Norah Jones wrote:
> 
> > I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?
> 
> 
> 
> Use numpy
> 
> 
> 
> import numpy as np
> 
> np.random.uniform(0, 5, 100)
> 
> 
> 
> # note that the values are from the interval [0, 5)
> 
> 
> 
> Maarten

While numpy would work, I fail to see how encouraging the op to download and install a separate library and learn a whole new set of tools would be beneficial by default, without knowing the purpose of the need.  This is like recommending an RPG to fix a sticky door hinge.

[toc] | [prev] | [next] | [standalone]


#41152

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-03-12 21:59 +0000
Message-ID<mailman.3252.1363125597.2939.python-list@python.org>
In reply to#41149
On 12 March 2013 20:21, llanitedave <llanitedave@veawb.coop> wrote:
> On Tuesday, March 12, 2013 10:47:25 AM UTC-7, Maarten wrote:
>> On Tuesday, March 12, 2013 6:11:10 PM UTC+1, Norah Jones wrote:
>>
>> > I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?
>>
>> Use numpy
[SNIP]
>
> While numpy would work, I fail to see how encouraging the op to download and install a separate library and learn a whole new set of tools would be beneficial by default, without knowing the purpose of the need.  This is like recommending an RPG to fix a sticky door hinge.

This suggestion comes after others that show how to use the stdlib's
random module. I don't think it's unreasonable to recommend numpy for
this. If you want to create *arrays* of random numbers then why not
use a library that provides an API specifically for that? You can test
yourself to see that numpy is 10x faster for large arrays:

Python 2.7 on Linux:
$ python -m timeit -s 'import random' -- '[random.uniform(0, 5) for x
in range(1000)]'
1000 loops, best of 3: 729 usec per loop
$ python -m timeit -s 'import random' -- '[random.random() * 5 for x
in range(1000)]'
1000 loops, best of 3: 296 usec per loop
$ python -m timeit -s 'import numpy' -- 'numpy.random.uniform(0, 5, 1000)'
10000 loops, best of 3: 32.2 usec per loop

I would use numpy for this mainly because if I'm creating arrays of
random numbers I probably want to use them in ways that are easier
with numpy arrays. There's also a chance the OP might benefit more
generally from using numpy depending on what they're working on.


Oscar

[toc] | [prev] | [next] | [standalone]


#41161

Fromllanitedave <llanitedave@veawb.coop>
Date2013-03-12 22:35 -0700
Message-ID<94642099-575e-4124-b0f6-43f26f880048@googlegroups.com>
In reply to#41152
On Tuesday, March 12, 2013 2:59:29 PM UTC-7, Oscar Benjamin wrote:
> On 12 March 2013 20:21, llanitedave <llanitedave@veawb.coop> wrote:
> 
> > On Tuesday, March 12, 2013 10:47:25 AM UTC-7, Maarten wrote:
> 
> >> On Tuesday, March 12, 2013 6:11:10 PM UTC+1, Norah Jones wrote:
> 
> >>
> 
> >> > I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?
> 
> >>
> 
> >> Use numpy
> 
> [SNIP]
> 
> >
> 
> > While numpy would work, I fail to see how encouraging the op to download and install a separate library and learn a whole new set of tools would be beneficial by default, without knowing the purpose of the need.  This is like recommending an RPG to fix a sticky door hinge.
> 
> 
> 
> This suggestion comes after others that show how to use the stdlib's
> 
> random module. I don't think it's unreasonable to recommend numpy for
> 
> this. If you want to create *arrays* of random numbers then why not
> 
> use a library that provides an API specifically for that? You can test
> 
> yourself to see that numpy is 10x faster for large arrays:
> 
> 
> 
> Python 2.7 on Linux:
> 
> $ python -m timeit -s 'import random' -- '[random.uniform(0, 5) for x
> 
> in range(1000)]'
> 
> 1000 loops, best of 3: 729 usec per loop
> 
> $ python -m timeit -s 'import random' -- '[random.random() * 5 for x
> 
> in range(1000)]'
> 
> 1000 loops, best of 3: 296 usec per loop
> 
> $ python -m timeit -s 'import numpy' -- 'numpy.random.uniform(0, 5, 1000)'
> 
> 10000 loops, best of 3: 32.2 usec per loop
> 
> 
> 
> I would use numpy for this mainly because if I'm creating arrays of
> 
> random numbers I probably want to use them in ways that are easier
> 
> with numpy arrays. There's also a chance the OP might benefit more
> 
> generally from using numpy depending on what they're working on.
> 
> 
> 
> 
> 
> Oscar

I don't think numpy is unreasonable for you or me.  I just started learning it recently, and I'm pretty jazzed about its possibilities.  I obtained an app for work that uses it, and now it's up to me to maintain it, so learning it is a good idea for me regardless.  Now I'm starting to fantasize about other things I could do with it.

But the OP appears like a pretty basic beginner, and I really think that for such a entry-level knowledge scale, we should stick to the standard library until they're ready to take on more sophisticated tasks.  "Premature Optimization" is the analogy that comes to mind.

[toc] | [prev] | [next] | [standalone]


#41162

Fromllanitedave <llanitedave@veawb.coop>
Date2013-03-12 22:35 -0700
Message-ID<mailman.3256.1363153648.2939.python-list@python.org>
In reply to#41152
On Tuesday, March 12, 2013 2:59:29 PM UTC-7, Oscar Benjamin wrote:
> On 12 March 2013 20:21, llanitedave <llanitedave@veawb.coop> wrote:
> 
> > On Tuesday, March 12, 2013 10:47:25 AM UTC-7, Maarten wrote:
> 
> >> On Tuesday, March 12, 2013 6:11:10 PM UTC+1, Norah Jones wrote:
> 
> >>
> 
> >> > I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?
> 
> >>
> 
> >> Use numpy
> 
> [SNIP]
> 
> >
> 
> > While numpy would work, I fail to see how encouraging the op to download and install a separate library and learn a whole new set of tools would be beneficial by default, without knowing the purpose of the need.  This is like recommending an RPG to fix a sticky door hinge.
> 
> 
> 
> This suggestion comes after others that show how to use the stdlib's
> 
> random module. I don't think it's unreasonable to recommend numpy for
> 
> this. If you want to create *arrays* of random numbers then why not
> 
> use a library that provides an API specifically for that? You can test
> 
> yourself to see that numpy is 10x faster for large arrays:
> 
> 
> 
> Python 2.7 on Linux:
> 
> $ python -m timeit -s 'import random' -- '[random.uniform(0, 5) for x
> 
> in range(1000)]'
> 
> 1000 loops, best of 3: 729 usec per loop
> 
> $ python -m timeit -s 'import random' -- '[random.random() * 5 for x
> 
> in range(1000)]'
> 
> 1000 loops, best of 3: 296 usec per loop
> 
> $ python -m timeit -s 'import numpy' -- 'numpy.random.uniform(0, 5, 1000)'
> 
> 10000 loops, best of 3: 32.2 usec per loop
> 
> 
> 
> I would use numpy for this mainly because if I'm creating arrays of
> 
> random numbers I probably want to use them in ways that are easier
> 
> with numpy arrays. There's also a chance the OP might benefit more
> 
> generally from using numpy depending on what they're working on.
> 
> 
> 
> 
> 
> Oscar

I don't think numpy is unreasonable for you or me.  I just started learning it recently, and I'm pretty jazzed about its possibilities.  I obtained an app for work that uses it, and now it's up to me to maintain it, so learning it is a good idea for me regardless.  Now I'm starting to fantasize about other things I could do with it.

But the OP appears like a pretty basic beginner, and I really think that for such a entry-level knowledge scale, we should stick to the standard library until they're ready to take on more sophisticated tasks.  "Premature Optimization" is the analogy that comes to mind.

[toc] | [prev] | [next] | [standalone]


#41151

Fromllanitedave <llanitedave@veawb.coop>
Date2013-03-12 13:21 -0700
Message-ID<mailman.3250.1363120310.2939.python-list@python.org>
In reply to#41140
On Tuesday, March 12, 2013 10:47:25 AM UTC-7, Maarten wrote:
> On Tuesday, March 12, 2013 6:11:10 PM UTC+1, Norah Jones wrote:
> 
> > I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?
> 
> 
> 
> Use numpy
> 
> 
> 
> import numpy as np
> 
> np.random.uniform(0, 5, 100)
> 
> 
> 
> # note that the values are from the interval [0, 5)
> 
> 
> 
> Maarten

While numpy would work, I fail to see how encouraging the op to download and install a separate library and learn a whole new set of tools would be beneficial by default, without knowing the purpose of the need.  This is like recommending an RPG to fix a sticky door hinge.

[toc] | [prev] | [next] | [standalone]


#41141

FromMaarten <maarten.sneep@knmi.nl>
Date2013-03-12 10:47 -0700
Message-ID<mailman.3242.1363110453.2939.python-list@python.org>
In reply to#41135
On Tuesday, March 12, 2013 6:11:10 PM UTC+1, Norah Jones wrote:
> I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?

Use numpy

import numpy as np
np.random.uniform(0, 5, 100)

# note that the values are from the interval [0, 5)

Maarten

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web