top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

0 votes
1,001 views

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?

posted Mar 12, 2013 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

[random.uniform(0,5) for i in range(100)]

[0.035440065542497456, 1.437405027400226, 4.3729265564939235,

1.8571876890801535, 3.3707291675828355, 3.8527038142772803,

2.335308526527048, 1.6648256912958126, 2.619282525564386,

0.49146229156297017, 0.44118757769151584, 4.739666518393803,

2.382053744691543, 0.49644235270002446, 3.2450874430280967,

2.907453418492667, 4.476790608458042, 3.6331854165844604,
4.048234752835737, 1.0561381241342283, 2.812909536326582,
3.561597391575344, 2.6487355099594017, 0.29397014028037627,
2.4479483428627753, 3.958448741888134, 2.407241234096458,
1.3214223763910538, 2.13697973410729, 0.5948251249983533,
1.7529836288331397, 1.5086813377327446, 1.8586362776340244,
1.2208704263132752, 0.641484635760266, 1.3848412838385726,
0.9293523709719054, 2.186001913964843, 4.573380203193875,
2.139476734752273, 2.9472883699144536, 2.896233361842901,
3.6862386168483736, 0.34731746668937247, 0.32240948705737016,
3.5558945043043533, 3.2122777306650474, 4.361615595368701,
0.015650980269780734, 3.6657002416980946, 2.559029702763296,
3.1821909947792215, 1.110074378492174, 4.631074891897119,
0.34141410223593516, 4.857392826027885, 3.527794364975918,
1.1557966421173278, 3.052715879227505, 3.5157974813529522,
1.1124961331040095, 0.3481541778415814, 4.669841649649461,
0.5971397176504589, 2.558151735886299, 1.2604807126742945,
2.281602331386756, 2.1519211043558695, 3.3468967934451657,
1.8240743647766071, 2.91696855571327, 0.6894263573879533,
2.7732038929294616, 4.783919829213994, 4.082864012400709,
0.16128311206877133, 4.959480373070126, 2.8458909583600187,
4.494888799994467, 4.647426388056034, 3.111088594459788,
4.261340689865024, 1.6013438490852865, 3.6386026965034852,
1.212916907042898, 3.3586184962657706, 3.6105733007635954,
0.5372141790624257, 0.9433843973095679, 3.113889114931214,
3.054082222169326, 2.360224809741029, 2.026697918525358,
1.322913986495805, 4.341848866805052, 0.970311202088483,
2.002058149505537, 0.07453277198439523, 1.9633241018322773,
4.22967258746455]

answer Mar 12, 2013 by anonymous
0 votes

You can use [random.random() * 5 for x in range(100)] but works only on range [0, 5). If you want to include 5, you will need more code.

Cheers,

answer Mar 12, 2013 by anonymous
Similar Questions
+1 vote

I have this Python code:

self.lock_tables("read", ['nets_permissions as n', 'devices_permissions as d'])
usrs = self.db.get("SELECT n.user_id FROM nets_permissions as n \
                    left join devices_permissions as d \
                    on n.user_id = d.user_id \
                    where d.user_id is null \
                    and n.network_id=%s and n.perm<>3", netid)
self.unlock_tables()

for usr in usrs:
    self.lock_tables("write", ['devices_permissions'])
    self.db.execute("INSERT devices_permissions SET \
                     user_id=%s, network_id=%s, device_id=%s, perm=%s",\
                     usr, netid, sensid, perm)
    self.unlock_tables();

I first do a query to retrieve some user_id from two tables. I want save this user_id in one variable and after do a for loop to insert this records in another table...

This code doesn't work. I obtain this error:    
Exception: Multiple rows returned for Database.get() query

How can I retrieve this multiple rows and then process everyone of them at one time?

+2 votes

How we can send mail with attachment in Python? Is it any prerequisite for it?

+1 vote

i want to create a chrome extension. its have complex mathematical queries. so i need to use python as scripting language. help me to implement python script in google chrome addon

0 votes

Hi,

I have a list of arbitrary length, and I need to split it up into equal size chunks. There are some obvious ways to do this, like keeping a counter and two lists, and when the second list fills up, add it to the first list and empty the second list for the next round of data, but this is potentially extremely expensive.

I was wondering if anyone had a good solution to this for lists of any length

This should work:

l = range(1, 1000)
print chunks(l, 10) -> [ [ 1..10 ], [ 11..20 ], .., [ 991..999 ] ]

I was looking for something useful in itertools but I couldn't find anything obviously useful.

Appretiate your help.

+2 votes

I'm trying to access Apples iCal-Server on a Mac OS X Snow Leopard Server via Python. The server is up and running and working with it via the iCal-Application is just fine. Now I need to access this server via Python to use it as backend for resource planning.

So how can I read the events within a time range from a user's calendar using python?

...