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


Groups > comp.lang.python > #41162

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <llanitedave@veawb.coop>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'float': 0.05; 'that?': 0.05; 'encouraging': 0.07; 'api': 0.09; 'subject:How': 0.09; 'python': 0.09; 'subject:create': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'subject:python': 0.11; '2.7': 0.13; 'library': 0.15; '100,': 0.16; '10x': 0.16; 'arrays.': 0.16; 'benjamin': 0.16; 'cc:name:python list': 0.16; 'numpy': 0.16; 'subject:array': 0.16; 'subject:random': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'appears': 0.18; 'app': 0.19; 'default,': 0.22; 'stick': 0.22; 'work,': 0.22; 'work.': 0.23; 'random': 0.24; 'idea': 0.24; 'cc:2**1': 0.24; 'specifically': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'creating': 0.26; 'values': 0.26; 'separate': 0.27; 'mind.': 0.27; 'module.': 0.27; 'arrays': 0.29; 'array': 0.29; 'probably': 0.29; 'this.': 0.29; "i'm": 0.29; 'install': 0.29; "skip:' 10": 0.30; 'basic': 0.30; 'generally': 0.32; "skip:' 20": 0.32; 'suggestion': 0.32; 'could': 0.32; 'knowledge': 0.33; 'received:google.com': 0.34; 'fail': 0.35; 'faster': 0.35; 'really': 0.36; 'but': 0.36; '12,': 0.36; 'test': 0.36; 'should': 0.36; 'does': 0.37; 'uses': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'things': 0.38; 'think': 0.40; 'chance': 0.61; 'door': 0.63; 'more': 0.63; 'show': 0.63; '10000': 0.65; 'tasks.': 0.65; 'benefit': 0.70; 'obtained': 0.71; 'yourself': 0.77; '2013': 0.84; 'oscar': 0.84; 'maarten': 0.91; 'ranging': 0.91; 'beneficial': 0.93
X-Received by 10.50.186.133 with SMTP id fk5mr2263674igc.0.1363152951844; Tue, 12 Mar 2013 22:35:51 -0700 (PDT)
Newsgroups comp.lang.python
Date Tue, 12 Mar 2013 22:35:51 -0700 (PDT)
In-Reply-To <mailman.3252.1363125597.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=75.14.221.102; posting-account=aw7wEQoAAACnaP8vftI9MyiC9NfXNJyr
References <mailman.3237.1363108626.2939.python-list@python.org> <0aa38f5a-0e5e-43cd-b6ba-69af6f37e94e@googlegroups.com> <bcb46e43-51c7-40ba-bd4a-3787ce9f8ceb@googlegroups.com> <mailman.3252.1363125597.2939.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 75.14.221.102
MIME-Version 1.0
Subject Re: How can i create a random array of floats from 0 to 5 in python
From llanitedave <llanitedave@veawb.coop>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc llanitedave <llanitedave@veawb.coop>, Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.3256.1363153648.2939.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363153648 news.xs4all.nl 6885 [2001:888:2000:d::a6]:43280
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41162

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web