Jump to content

[SOLVED] PHP to select chance - 50% of time X - 50% of time Y


doa24uk

Recommended Posts

Hi guys,

 

I'm sure this is very simple, and I believe has something to do with generating random numbers (although I could be wrong).

 

What I need is simple, code that will show picture X 50% of the time, and picture Y 50% of the time. So that  the views on each picture will be exactly the same.

 

Can this be achieved? Thanks in advance

 

DoA

Link to comment
Share on other sites

You're talking about showing pic1.jpg on 50% of pageloads and pic2.jpg on the other 50% of pageloads?

 

If you don't care what order they show in, then probability surely does dictate that a random selection between 0 and 1 will produce "1" 50% of the time and "0" the remaining 50% of the time, which means that if you use that to select an image source, pic1.jpg will indeed show on 50% of pageloads and pic2.jpg will show on 50% of pageloads.

 

Maybe I missed the question? What does "shown 50% evenly" mean?

Link to comment
Share on other sites

Are you wanting these images to show up evenly for each specific user, or just by click? If it is by click, I would suggest just making them show up randomly and not worry about if it is being shown evenly.

 

If you are wanting to make sure it shows up evenly for each specific user, then you are going to have to use a database to keep track of which one the user saw last.

Link to comment
Share on other sites

Do understand that random choice between two alternatives will not produce what you want except over an enormous number of iterations.

 

If you flip a coin and it lands heads up, will the next flip be tails?  No, it'll be random because the coin has no memory of what happened before and heads is equally as likely as tails.

 

The only way to show 'the other picture' the next time is to know what picture was seen last time - and that means storing information (flat file, cookie, database).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.