I have a table with for example the following columns:
– id
– title
– body
– type (image, swf, text)
– posted_on
Lets say there are over 3 million rows in this table.
I need to get 1 random row from within this 3 million.
Obviously using the ORDER BY RAND() is not a good idea and is extremely slow.
Another way would be getting the max_id then doing a random from 1 to max_id (call it $random_num) and then getting a query which states give me item >= $random_num LIMIT 1
This may seem to work and will work great if there were no id’s deleted.
It becomes very tricky now that I add a WHERE clause stating (for example) WHERE `type
Let me explain (id’s below):
1 = text
2 = text
3 = image
4 = flash
5 = image
6 = text
7 = text
In the above case id #1 has a 1:7 chance to get selected while id #6 has a 4:7 chance since items 3,4, and 5 if selected would also cause #6 to be picked.
Basically I need to know what is the appropriate method of getting a random id in cases that I will be filtering using a WHERE statement.
Thanks.
index type_randomindex (type, random_index)[/QUOTE]
Lets say there are over 3 million rows in this table.
I need to get 1 random row from within this 3 million.
Obviously using the ORDER BY RAND() is not a good idea and is extremely slow.
[/QUOTE]
[code=php]
$randomRow = mysql_fetch_assoc(
mysql_query(
sprintf(
"SELECT * FROM User LIMIT %d,1",
rand(1,implode(mysql_fetch_row(mysql_query("SELECT COUNT(User_ID
) FROM User"))))
)));
var_dump($randomRow);
[/code]
PK
This may seem to work and will work great if there were no id's deleted. [/QUOTE]
Basically I need to know what is the appropriate method of getting a random id in cases that I will be filtering using a WHERE statement.[/QUOTE]
So are you basically saying:
Make a SELECT query with all the filters and only get ID's, place them in an array, random one array and get the info from there?
Basically make an index using array's?[/QUOTE]
Random numbers aren't actually random different numbers. They are numbers chosen not in order, but they don't have to be different. Basically numbers chosen by probability, numbers that are chosen differently all the time are actually chosen by a pattern, this is what algorithms are for. they create different situations chosen by set(consistent) parameters. so random isn't really random. just out of order.
to create an actual random(different all the time) you have to create a pattern to differentiate between the numbers. basically variables that change to create a constant difference. something like below will guarantee a different number all the time. you might have to tweak to create the difference you want.
$random = rand(1,3000000)
$random2 = rand(2,3000000) the 2 start creates a consistent difference.
($random * 456/$random2) = always a completely random number. But as I was stating you basically have a pattern in the equation. Funny how math works. if I knew php better I would be able to explain how to use this but I'm only a beginner.
the funny thing you will also notice about real random-ness is that most people don't really want it. we all like a little consistency even in our coding. You should really focus on what you want then try to create that situation. Random is kind of a hoax and you'll get tired of it real soon....you'll see.[/QUOTE]
What a random thing to say..[/QUOTE]
So, there was an equal probability that he would have said anything else from the set of all things he can say?[/QUOTE]
yeah that comment happened to stored in row 27,918.[/QUOTE]
...
Chaos as you corrected me will allow for his whole database to be chosen a long time before repeating again, but still with in the set amount and still allowing for some repetition. Giving all in his DB an close to equal chance. Eliminating what ever issues you think there are mathematically or philosophically.
1) Chaos is basically a better form of random. If you want all to have a chance.
2) Random is set selection, not probability. it will chose what it wants. Not chose within what it can. That is what chaos is. Random will chose to show 5 out of ten in ten tries if it so chooses too. Chaos will chose each number in a more proportionate manner and not by selection, but with in its range.
So if he wanted to have each row shown and with as little repetition as possible he would chose chaos or an simple equation to force numbers that vary widely as in my example.
...[/QUOTE]
Xerosis:
"It becomes very tricky now that I add a WHERE clause stating (for example) WHERE= 'text'; all of a sudden this method becomes unfair and chances of some numbers coming up more becomes much higher."[/Quote] type
1) Random has nothing to do with equal distribution or chance that is probability. Random selects in an unordered manner, this is actually happening, But it selects with repetition, so even if you ran an example from 1-3million numbers there would be selected numbers repeated in an order, which limits selection.
He wants each row to be shown equally, chaos or True randomness will accomplish this.
2)True Randomness which is chaos means selecting without repetition but in an unordered manner. Of course when the range is met it will begin again, which in fact is repetition.
So in chaos form anywhere within the range a number will be selected, in randomness the numbers will be selected and repeated. That is what I assume he doesn't want. But I guess PHP runs differently.
Whatever solution works for him is great thats what matters. And the equation was to help him select form his db more evenly with out repeating.[/QUOTE]
Random sampling can also refer to taking a number of independent observations from the same [B]probability distribution[/B], without involving any real population.[/quote]
And stop saying randomness has something to do with chance. that is probability.[/QUOTE]
Yeah your right it only took me to the second line to prove my point:
Quote:
Random sampling can also refer to taking a number of independent observations from the same probability distribution[/QUOTE]
... A simple random sample is selected so that all samples of the same size have an equal chance of being selected from the population.
A self-weighting sample, also known as an EPSEM (Equal Probability of Selection Method) sample, is one in which every individual, or object, in the population of interest has an equal opportunity of being selected for the sample. Simple random samples are self-weighting.
... etc.[/QUOTE]
PS ... Did you also happen to read past the line the "proved your point" ... ?[/QUOTE]
Can you reset my password on monkeywar.thepointless.com[/QUOTE]
Xerosis:
"It becomes very tricky now that I add a WHERE clause stating (for example) WHERE= 'text'; all of a sudden this method becomes unfair and chances of some numbers coming up more becomes much higher."[/Quote] type
[B]Types of random sample[/B]
A [B]simple random sample[/B] is selected so that all samples of the same size have an equal chance of being selected from the population.
A [B]self-weighting sample[/B], also known as an EPSEM (Equal Probability of Selection Method) sample, is one in which every individual, or object, in the population of interest has an equal opportunity of being selected for the sample. Simple random samples are self-weighting.
[B]Stratified sampling[/B] involves selecting independent samples from a number of subpopulations, group or strata within the population. Great gains in efficiency are sometimes possible from judicious stratification.Cluster sampling involves selecting the sample units in groups. For example, a sample of telephone calls may be collected by first taking a collection of telephone lines and collecting all the calls on the sampled lines. The analysis of cluster samples must take into account the intra-cluster correlation which reflects the fact that units in the same cluster are likely to be more similar than two units picked at random.[/Quote]
Listen if you have the intention of helping someone then just do that but don't try to force your opinion with false or out of context information.[/QUOTE]
This has nothing to do with sampling... we know the entire population.[/QUOTE]
... get it done with rand() and stop joking around ?[/QUOTE]
There is no such thing as random anyways- only destiny.[/QUOTE]
0.1.9 — BETA 5.3