Answer the question
In order to leave comments, you need to log in
How to make at least close to random random on the site?
There is a task: randomly show one of several blocks when reloading the site pages.
When using standard php tools, only one of the blocks is shown in 99% (there are only 2 of them now).
How to cure?
Answer the question
In order to leave comments, you need to log in
For such a task, you can do it quite simply:
$blockId = time() % 2; // $blockId = 0 или $blockId = 1
It can be assumed that the random counter is not “untwisted”. Try inserting before random srand((double)microtime()*1000000)
for example.
For some reason, I immediately thought that your output is cached and the same thing is given all the time.
I am using this algorithm.
There are several blocks, one of which must be shown in a certain place on the site.
Each block has a weight that affects the probability of a block appearing.
We add up the weight of all blocks, in parallel we form a matrix of correspondence of a random number to a block,
we generate a random number from 0 to the total weight of all blocks minus 1.
Based on the resulting number, we take the block index from the correspondence matrix.
The correspondence matrix is filled at the stage of calculating the total weight of the blocks
, for example, block 1 has a weight of 3, block 2 has a weight of 3, block 3 has a weight of 1, the matrix will look like this.
$m3x = Array(
0 => 1,
1 => 1,
2 => 1,
3 => 2,
4 => 2,
5 => 2,
6 => 3,
);
everything works great.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question