multiple random text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tecton
Forum Newbie
Posts: 22
Joined: Sat Dec 20, 2003 1:58 am
Contact:

multiple random text

Post by tecton »

kind of a strange request

i need to figure out how to obtain multiple random text strings from a single page display...

all the random text scripts i can find only run once, i need something that i can run 5-6-7-8 times on a single page...each time producing a new random string

any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

single page display of.. html, plain text, what?
new random string of any line? or just random offsets into the file? please explain a bit more.
tecton
Forum Newbie
Posts: 22
Joined: Sat Dec 20, 2003 1:58 am
Contact:

Post by tecton »

html


i need a new random number to be created each time i call for it in a single page

lets say i call $random
$random = 25

if i call random again, i will have 2 "25"'s
i dont want that
i want to be able to call it multiple times, and get different results each time
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]rand[/php_man] or [php_man]mt_rand[/php_man] can do this..
tecton
Forum Newbie
Posts: 22
Joined: Sat Dec 20, 2003 1:58 am
Contact:

Post by tecton »

this might work

but i will need it to only pick between 2 numbers

1 and 0

1 character long
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's not likely to loop within a short period of time unless it's called a LOT.. you can use those 2 functions with [php_man]uniqid[/php_man] to get a nice big random if you need it closer to truely random.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you just want to choose between 1 and 0 or whatever boolean level thing I do this all the time:

Code: Select all

$choice = mt_rand() % 2
tells you whether mt_rand returned an even or odd number :) (even = 0)

I use mt_rand because it has a MUCH longer (2^19937-1) repeat period than rand and 4 times faster..
tecton
Forum Newbie
Posts: 22
Joined: Sat Dec 20, 2003 1:58 am
Contact:

Post by tecton »

but can i run that multiple times in a single page without returning the same result
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

correct.. if you want to keep that result throughout the page, just save it into a variable.
tecton
Forum Newbie
Posts: 22
Joined: Sat Dec 20, 2003 1:58 am
Contact:

Post by tecton »

hehe

i dont, thats the point

i want a new random number, each time i call the script, possibly multiple times on a single page
8)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

then just call [php_man]mt_rand[/php_man] or whatever every time you want it. what's the big deal?
tecton
Forum Newbie
Posts: 22
Joined: Sat Dec 20, 2003 1:58 am
Contact:

Post by tecton »

the big deal is i dont know how to call that
2 numbers
0 or 1
then use that to call something else

like 0.gif
1.gif
i need it in variable form i would say for that....this is just getting more and more out of my hands
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

echo (mt_rand() % 2) . '.gif';
:?
Post Reply