doa24uk Posted June 28, 2007 Share Posted June 28, 2007 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 Quote Link to comment Share on other sites More sharing options...
kael.shipman Posted June 28, 2007 Share Posted June 28, 2007 Random indeed: <?php $pictures = array('src/of/pic1.jpg','src/of/pic2.jpg'); $showPic = $pictures[rand(0,1)]; ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 28, 2007 Share Posted June 28, 2007 Random indeed: <?php $pictures = array('src/of/pic1.jpg','src/of/pic2.jpg'); $showPic = $pictures[rand(0,1)]; ?> That would randomly generate one of the images....which would mean they would not be shown 50% evenly. Quote Link to comment Share on other sites More sharing options...
kael.shipman Posted June 28, 2007 Share Posted June 28, 2007 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? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 Make a flat file that stores a value last image shown and open it and each time switch the value if you want "50/50" or use a mysql table line Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 28, 2007 Share Posted June 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
AndyB Posted June 28, 2007 Share Posted June 28, 2007 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). Quote Link to comment Share on other sites More sharing options...
doa24uk Posted June 28, 2007 Author Share Posted June 28, 2007 Yes, sorry I should have clarified; They don't have to show up sequentially by 50% (a,b,a,b,a etc.) they can indeed by (a,a,b,b,b,a) So the first example will work great, thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.