• You are not logged in. | Login

Post a reply

March 21, 2007 8:04 am

ARRAY_RAND Values are repeated

A script consists of big number of cycles.
There are big massives (containing 4000 values).

$new_array = $xx_array[array_rand($xx_array)];

It displays a big number of repeated values in the cycle.
I’ve tried on hosting but it’s the same…

How to evade values repeating 2-5 times?
Here is the massive of displayed numbers:

2555
6783
1240
11334
4597
7227
6690
11334
7227
4496
2555
6903
4496
2555
1240
11229
7227
6690
11334
7227
2555
6903

and so on.
And this is random in the range from 1 to 11225 numbers…

Tell me please how to get rid from permanent ‘repetitions’?


 

 

March 21, 2007 8:10 am

yendii
Member
Ranks

Re: ARRAY_RAND Values are repeated

array_unique?


 

 

March 21, 2007 8:14 am

Re: ARRAY_RAND Values are repeated

array_unique?

No as far as the function doesn’t ‘remember’ previous values.
I need to generate 50 squares with different background colors and different size…
The function is called in a cycle and it contains numbers itself so finally everything is repeated 3-5 times!


 

 

March 21, 2007 8:17 am

mambojim
Member
Ranks

Re: ARRAY_RAND Values are repeated

isset


 

 

March 21, 2007 8:20 am

napkdd1
Member
Ranks

Re: ARRAY_RAND Values are repeated

What a sophistication  smile

shuffle. And not in cycle, of course. Once is enough.


 

 

March 21, 2007 8:22 am

Re: ARRAY_RAND Values are repeated

What a sophistication  smile

shuffle.

Yes, shuffle suits but wouldn’t it demand a lot of memory?
15000 massive values each of which contains 100 symbols?


 

 

March 21, 2007 8:23 am

n00bphp
Member
Ranks

Re: ARRAY_RAND Values are repeated

PilgrimFarAway, where have you found ‘15000 massive values each of which contains 100 symbols’ mentioned?


 

 

March 21, 2007 8:26 am

napkdd1
Member
Ranks

Re: ARRAY_RAND Values are repeated

PilgrimFarAway, I haven’t viewed the sources but I suppose that shuffle assigns random keys and then makes sorting. You may try doing the same with rand, isset and then sort and look which is faster  wink


 

 

March 21, 2007 8:30 am

Re: ARRAY_RAND Values are repeated

where have you found ‘15000 massive values each of which contains 100 symbols’ mentioned?

Really, it wasn’t mentioned…
Generally shuffle is convenient to use. But you are not to exceed memory_limit.

P.S. I haven’t tested it yet but it’s possible that function rand(x,y); will also display ‘repeating’ values.


 

 

March 21, 2007 8:34 am

sirburpsalot
Member
Ranks

Re: ARRAY_RAND Values are repeated

PilgrimFarAway, each generator of pseudo random numbers should display repeated values. Otherwise they won’t be random. Probability of random number appearance doesn’t depend on the prehistory.


 

 

July 13, 2007 5:55 am

prisharma
Member
Ranks

Re: ARRAY_RAND Values are repeated

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>


 

 
  • Actions
  • Top
ITCrimea. Ukraine Web Development Company. Professional Developers and Web Designers Team
Custom Web Designs, Internet Applications, E-Commerce Websites, Interactive Sites, Database-Driven Sites and Services