Jump to content

How to make mt_rand more random? PHP

I have a site that I use mt_rand for a button to pick a random video but it doesn't seem all that random.

 

It will pick sometimes the same video 2 times in a row when there's over 70 on the site.

 

Also if anyone has a JavaScript alternative let me know.

i want to die

Link to comment
https://linustechtips.com/topic/96368-how-to-make-mt_rand-more-random-php/
Share on other sites

Link to post
Share on other sites

I have a site that I use mt_rand for a button to pick a random video but it doesn't seem all that random.

 

It will pick sometimes the same video 2 times in a row when there's over 70 on the site.

 

Also if anyone has a JavaScript alternative let me know.

just because it picks 2 times thesame vid in a row doesnt mean that its not random. if you throw a dice twice, odds are, it will be thesame number. same goes for that.

Intel 3570k 3,4@4,5 1,12v Scythe Mugen 3 gigabyte 770     MSi z77a GD55    corsair vengeance 8 gb  corsair CX600M Bitfenix Outlaw 4 casefans

 

Link to post
Share on other sites

just because it picks 2 times thesame vid in a row doesnt mean that its not random. if you throw a dice twice, odds are, it will be thesame number. same goes for that.

yes but picking a number 1-70+ 2 times in a row doesn't happen often with items like dice. 

i want to die

Link to post
Share on other sites

yes but picking a number 1-70+ 2 times in a row doesn't happen often with items like dice. 

but it can happen.

im afraid i cant help you any further, i dont know any php.

Intel 3570k 3,4@4,5 1,12v Scythe Mugen 3 gigabyte 770     MSi z77a GD55    corsair vengeance 8 gb  corsair CX600M Bitfenix Outlaw 4 casefans

 

Link to post
Share on other sites

yes but picking a number 1-70+ 2 times in a row doesn't happen often with items like dice.

If I remember my math's correctly, every time eg. You throw a die you have 1/6 chance to get a number so getting 1+2 is just as possible as getting 1+1

Stock coolers - The sound of bare minimum

Link to post
Share on other sites

I have a site that I use mt_rand for a button to pick a random video but it doesn't seem all that random.

 

It will pick sometimes the same video 2 times in a row when there's over 70 on the site.

 

Also if anyone has a JavaScript alternative let me know.

 

By the looks of things mt_rand should be quite random (that is assuming you are using mt_rand(min, max), even if you aren't it is still adequately random).  The mt_rand based on the documentations I read use the Mersenne twister primes (http://en.wikipedia.org/wiki/Mersenne_twister).%C2'> The MT is fairly standard random number generator

 

Anyways just to clarify a few things...there is a 1 in 70 chance of repeating a video in a row.

1/70 * 1 / 70 * 70 = roughly 1.4% chance. 

With that said if you test it over 10 new random video views, that is 1 - (69/70)^10 = 13.4% of the time you will get a video that repeats itself.

 

A possible solution would just keep track with a cookie the last 3 or 4 videos and make sure the random doesn't choose them...

0b10111010 10101101 11110000 00001101

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×