2 piece randomisation script.

SpaceshipSpaceship
Watch

PoorDoggie

Soon to be RICHdoggie!VIP Member
Impact
18
Hi,

I need to write a script that will show a string 50% of the time and another string the other 50% of the time, but not always one after the other.

ie: I don't want someone to be able to anticipate which one is coming next.

so, can anyone help me randomise two strings, lets call them str1 and str2? I just don't know very much about randomisation. (well, nothing in fact)

They can't be bias though, the probability of either showing up should be 50%.

Thanks
Tom
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
PHP:
if(rand(0,1) == 0)
    echo $str1;
else
    echo $str2;

Should work for just 2 strings
 
0
•••
that simple huh?

Thanks a lot! :)
 
0
•••
PHP:
echo (mt_rand(0, 1) == 0) ? $str1 : $str2;

Cleaner :p :) Sorry, had to :guilty:
 
0
•••
:lol: what is the difference between mt_rand and rand?
 
0
•••
It is 4 times faster.
 
0
•••
SecondVersion said:
PHP:
echo (mt_rand(0, 1) == 0) ? $str1 : $str2;

Cleaner :p :) Sorry, had to :guilty:

it maybe cleaner but harder to read.
 
0
•••
not really, but the point is that I define a variable before and then if rand(1,0)==0 then I change it, so I can't do it the second way! :)

Thanks
Tom
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back