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

Gary Herron gherron at digipen.edu
Tue Mar 12 13:26:35 EDT 2013


On 03/12/2013 10:11 AM, 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?
>
>


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


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418




More information about the Python-list mailing list