php - Select a random row but with odds -
I have a dataset for each row with numbers of 'odds' between 1 and 100. I think it is the most effective way possible to add constraints to 100 obstacles.
I have some ideas.
a) Select the entire dataset and then add all the constraints and generate a random number in between. 1 and that number again reduces the number from the loop number through the dataset until it does not hold to 0.
I hope that the database is expected to minimize the impact, so I believe that I can only select the rows I needed.
b)
Select from WHERE table (100 * RAND ()) & lt; Obstacles
I believe LIMIT 0,1
but if the item has the same probability then only one will be returned
Alternatively, take the entire dataset and choose random one from there ... But then the obstacles are affected because it becomes random with obstacles and then becomes random without any obstacles , So the obstacles are tilted in favor of higher barriers (and also many Uc).
I think I can order from the barriers
then take the entire dataset and then take the rows together with PHP randomly before with the identical constraints (record) All time low).
Looks like a clumsy solution.
Is anyone having a better solution? If not, who is the best from above?
Do some up-front work, add some columns to your table that helps in selection. For example, suppose you have these rows
x2w3z1
we add some cumulative values
key barriers start end x 2 0 1 // range 0-> 1, 2 value == Obstacles Y 3 2 4 // Category 2-> 4, 3 value == Obstacles Z 1 5 5 // Category 5- & gt; 5, 1 value == barriers
The beginning and ending is chosen as follows. The beginning of the first row in the first row is greater than the beginning of the zeros. The end is (start + odd-1).
Now select a random number from range 0 to Max (end)
to select * where r> = T. Start and R & lt; = T If the database is sufficiently clever then we can use the select from * where the R & G; = T. Start and R & Lt; = (T.Start + T.Odds - 1)
I'm guessing that having an end column with the index could be a better performance than that, Somewhere else is triggered by the trigger when necessary
There is some difficulty in updating the start / end clearly
- The table content is stable
< Li> Or in any way the order is inherently ordered, so that each new row is still old It continues from the highest.
Comments
Post a Comment