Article View: chile.comp.programacion
Article #3278Re: duda numeros aleatorios php
From: Cristian Gutierrez
Date: Fri, 01 Apr 2005 02:22
Date: Fri, 01 Apr 2005 02:22
53 lines
3653 bytes
3653 bytes
Hoy en la tarde, Rick oj dijo:
[...]
>>> $numeros=array_fill(0,21,rand(1,100));
[...]
>>>estoy utilizando mal la funcion array_fill ?
>>
>> Exacto. array_fill no va a llamar a rand(1,100) 20 veces, sino que el
>> interprete de PHP llama a rand(1,100), obtiene un valor, y ese es el que
>> se lleva array_fill. Recuerda se evalua "de adentro hacia afuera".
>
> grax, tampoco serviria crear una funcion a ser llamada ? por que tambien
> estaria entregando el mismo numero
Claro, solo te serviria cambiar array_fill por una que en vez de recibir
un valor como segundo parametro, reciba una funcion a llamar cada
vez. Algo asi:
,----
| function my_array_fill($start, $end, $function) {
|
| $newarray = array();
| for ($i = $start; $i < $end; $i++)
| $newarray[$i] = call_user_func($function);
|
| return $newarray;
| }
|
| function my_function() {
| return rand(1,100);
| }
|
| $numeros = my_array_fill(0, 21, 'my_function');
`----
Se puede hacer un poco mas generico con una my_function no tan
"cableada", sino que se construya con parametros para los limites del
rand(..). Para eso tendrias que usar "create_function".
PS: todo esto se aplica -en primera instancia- solo a PHP. En algunos
otros lenguajes las funciones son tratadas como "ciudadanos de
primera clase", y no hay que andar haciendo tantos malabares para
pasar funciones como parametros (pero no como strings!) y despues
llamarlas.
--
Cristian Gutierrez http://www.dcc.uchile.cl/~crgutier
crgutier[@]dcc.uchile.cl Jabber:crgutier@jabber.org
En Intel, el control de calidad es la preocupaci�n numero 0.999987
Technical Details
Message-ID:
<87mzsjxmkl.fsf@guti.no-ip.org>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.kjsl.com!news1.nivel5.cl!news.cec.uchile.cl!helcaraxe.dcc.uchile.cl!guti.no-ip.org!newsReferences:
<d2hg51$mt2$1@news1.nivel5.cl> <87wtrnxzzh.fsf@guti.no-ip.org> <d2i2p6$sj1$1@news1.nivel5.cl>Show Full Headers
Complete Article Headers
X-Google-Thread: 1593cb,75664339df4cdcff X-Google-Attributes: gid1593cb,public X-Google-Language: SPANISH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.kjsl.com!news1.nivel5.cl!news.cec.uchile.cl!helcaraxe.dcc.uchile.cl!guti.no-ip.org!news From: Cristian Gutierrez <crgutier@dcc.uchile.cl> Newsgroups: chile.comp.programacion Subject: Re: duda numeros aleatorios php Date: Thu, 31 Mar 2005 22:22:50 -0400 Organization: DCC - Universidad de Chile Lines: 53 Message-ID: <87mzsjxmkl.fsf@guti.no-ip.org> References: <d2hg51$mt2$1@news1.nivel5.cl> <87wtrnxzzh.fsf@guti.no-ip.org> <d2i2p6$sj1$1@news1.nivel5.cl> NNTP-Posting-Host: anakena.dcc.uchile.cl Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: helcaraxe.dcc.uchile.cl 1112322604 6953 192.80.24.6 (1 Apr 2005 02:30:04 GMT) X-Complaints-To: newsmaster@dcc.uchile.cl NNTP-Posting-Date: Fri, 1 Apr 2005 02:30:04 +0000 (UTC) Face: iVBORw0KGgoAAAANSUhEUgAAADwAAAA8AgMAAABHkjHhAAAADFBMVEUWFhZfX1+ioqL6+vr8 dj+QAAAAAWJLR0QAiAUdSAAAAg5JREFUeNql0s9r02AYB/B4GIHuoAgVVmG9CVYwf4ILrht40Ynd dEXioWUsA3PaQQ8LKHpYLnbTThwiHbWYV9Sj9rL36NUObGkPUrVzuIs/6BvyluZ9ljcxaXI2lzwf eJOH9/k+AvhP/7j/Fv65OxMzmynhqO1zH/So+29NLeqvh6YU8WADmemIP4lxL8pxp6p3o2aZegZl R3YyaBJNaaGHKkqgxOXQNkKXzLQR+g9C6+b5kfcQevgmpYRuIrRZn5BCryO0US3ogW1BdV0J+3dz grpXmQXYfuf5nkLH5ksK/SIrnq/rg26ioTmdygtupwgWHSvoTnvF4CZl17dOYNoWy9yNPOzXfstg tYtXuR9o0JwlL+HgeWGBW8ZgGBbFnWUhw33Sbdz6TvHN1ey8a3YGIN+6T2C5PYFdD2sA6d5FAu9v LH3WBbAJBgmIDJ3ioooFoCzPNDq5in82TX7egsdXcE+0pZ3sEr+fxW7XoCvS3HRS4f7rrGHojRP0 6A72TOtuoBrZnVvz5kGIzVejX0jpvi2bF0Q+iz1TWuXFwbOkP7/hrrdaH58u+HakX7wwSsF8/eDT wf45417kFwIPd7yIcWDS52UDApevuZ86p0PLK1tuZHpoBN8YLgOM9pswfft/vO9oMf9g6UbMYNij /jDYglZwHRCYIIgx56aO4earyPlD89R0Mvo/9kR9HfgIgorhXaQc58oAAAAASUVORK5CYII= User-Agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:0kLzPzqF1ZSDHeggZTU+Gcqhk/A=