P
P
Pavel Bersenev2012-01-13 11:16:28
PHP
Pavel Bersenev, 2012-01-13 11:16:28

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

7 answer(s)
O
Ogra, 2012-01-13
@Ogra

For such a task, you can do it quite simply:

$blockId = time() % 2; // $blockId = 0 или $blockId = 1

Of course, you should not use such an algorithm for other tasks;)

N
NeX, 2012-01-13
@NeX

show the code that you think is random

N
nojoke, 2012-01-13
@nojoke

It can be assumed that the random counter is not “untwisted”. Try inserting before random srand((double)microtime()*1000000)for example.

T
TrueDrago, 2012-01-13
@TrueDrago

For some reason, I immediately thought that your output is cached and the same thing is given all the time.

D
DorBer, 2012-01-13
@DorBer

Use a wider range:

F
Fastto, 2012-01-13
@Fastto

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.

P
Pavel Bersenev, 2012-01-13
@wilelf

Thanks a lot.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question