TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Random Text (http://www.talkphp.com/general/2770-random-text.html)

Orc 05-07-2008 11:18 AM

Random Text
 
How would I make something this where it doesnt repeat the same words, like so:

I took my dog for a walk
into

took dog my for I a walk


not took took my my for for I a a walk walk

sketchMedia 05-07-2008 11:36 AM

not very pretty, took about 1min to knock up xD so theres most likely to be mistakes or easier ways:
PHP Code:

$words 'I took my dog for a walk';
$wordArr explode(' '$words);
$cnt count($wordArr);
$randNumStor = array();
//generate an array of random numbers
//make sure that no duplicates exist
for($i 0$i <= $cnt$i++)
{
    
$randNum rand(0$cnt);

    while(
in_array($randNum$randNumStor))
    {
        
$randNum rand(0$cnt);
    }
    
$randNumStor[] = $randNum;
}
//display the words based the random numbers in random array
for($j 0$j <= $cnt$j++)
{
    echo 
$wordArr[$randNumStor[$j]], ' ';


hope i havnt got the wrong end of the proverbial stick.

Edit: i think i may have gotten the wrong idea of you probelm, i just quickly scan read it and saw random text, sorry if i have got the wrong idea but still there are some things in that script that you could use
to delete duplicates from an array use array_unique()

for example:
PHP Code:

$words2 'I I took took my my dog dog for for a a walk walk';

$wordArray2 array_unique(explode(' '$words2));

foreach(
$wordArray2 as $word)
{
    echo 
$word ' ';


EDIT: no no it appears i have confused myself twice haha reality and fiction have now become a mad twist in my brain hahahahahahaha.... more coffee needed!

Orc 05-07-2008 11:40 AM

Quote:

Originally Posted by sketchMedia (Post 14392)
not very pretty, took about 1min to knock up xD so theres most likely to be mistakes or easier ways:
PHP Code:

$words 'I took my dog for a walk';
$wordArr explode(' '$words);
$cnt count($wordArr);
$randNumStor = array();
//generate an array of random numbers
//make sure that no duplicates exist
for($i 0$i <= $cnt$i++)
{
    
$randNum rand(0$cnt);

    while(
in_array($randNum$randNumStor))
    {
        
$randNum rand(0$cnt);
    }
    
$randNumStor[] = $randNum;
}
//display the words based the random numbers in random array
for($j 0$j <= $cnt$j++)
{
    echo 
$wordArr[$randNumStor[$j]], ' ';


hope i havnt got the wrong end of the proverbial stick.

Edit: i think i may have gotten the wrong idea of you probelm, i just quickly scan read it and saw random text, im keeping it up if i am mistaken, if i have got the wrong idea there are some things in that scrupt that you could use

Works, thanks! but how did it work exactly? its just loops it seems with just adding new elements inside variables. :P

sketchMedia 05-07-2008 11:53 AM

on the face of it it may sem complex but its actually quite simple (que pseudo(ish)code)

make an array of words
count how many words we have
loop over and make a random number
check the number to see if it already is in the random number array
if not continue, but if it does exist loop until a random number is produced that is unique

then loop over again but use the random number from the random number array as an index for the word array and echo it

make sense?

Orc 05-07-2008 11:56 AM

Quote:

Originally Posted by sketchMedia (Post 14394)
on the face of it it may sem complex but its actually quite simple (que pseudo(ish)code)

make an array of words
count how many words we have
loop over and make a random number
check the number to see if it already is in the random number array
if not continue, but if it does exist loop until a random number is produced that is unique

then loop over again but use the random number from the random number array as an index for the word array and echo it

make sense?

yeah yeah, just i havent felt well for the past few days, and well i cant think right, and plus my pc is being slow. too much memroy usage i ugess.

but cant you just use array_unqiue for making them unique words aswell?

sketchMedia 05-07-2008 11:58 AM

no because array_unique filters the array and removes duplicates, therefore it wont return anything we can use to check against, in otherwords you would have to loop alot more and have the array filtered and maybe not end up with enough random numbers, if that makes sense?

Orc 05-07-2008 12:01 PM

Quote:

Originally Posted by sketchMedia (Post 14396)
no because array_unique filters the array and removes duplicates, therefore it wont return anything we can use to check against, in otherwords you would have to loop alot more and have the array filtered and maybe not end up with enough random numbers, if that makes sense?

i guess, really i feel like a donkey right now. plus my head feels like a baloon. :Ss

sketchMedia 05-07-2008 12:03 PM

hehe been there mate, grab yourself a coffee or a redbull (as i do) and be as high as a kite (as i am atm) but i guess if your ill sleep may be a better remedy

Orc 05-07-2008 12:05 PM

Quote:

Originally Posted by sketchMedia (Post 14398)
hehe been there mate, grab yourself a coffee or a redbull (as i do) and be as high as a kite (as i am atm) but i guess if your ill sleep may be a better remedy

ill probably go play some gta 4 mp cause my head is about to explode(' brains ', $orc); from this flu or whatever it is. -_ but I'll go gte a coffee since redbull is like 10 dollars here.

Salathe 05-07-2008 12:07 PM

sketch, your first code snippet has an off-by-one error related to the $cnt variable. It needs to be count()-1 for the loops and calls to rand else you'll get "Undefined offset" notices.

Also, what's the harm in using something like the following instead of the longer, looping code snippet?
PHP Code:

$words 'I took my dog for a walk';
$shuffled explode(' '$words);
shuffle($shuffled);
echo 
implode(' '$shuffled); 


Orc 05-07-2008 12:10 PM

Quote:

Originally Posted by Salathe (Post 14400)
sketch, your first code snippet has an off-by-one error related to the $cnt variable. It needs to be count()-1 for the loops and calls to rand else you'll get "Undefined offset" notices.

Also, what's the harm in using something like the following instead of the longer, looping code snippet?
PHP Code:

$words 'I took my dog for a walk';
$shuffled explode(' '$words);
shuffle($shuffled);
echo 
implode(' '$shuffled); 


Niw, to be honest, thats what I was thinking all along, but I didn't think it work :S

sketchMedia 05-07-2008 12:15 PM

yea thanks your correct Salathe i overlooked that, infact i didnt think it through properly (as i previously said) tend to loose track of index offsets for some reason, i guess my brain just brushes it aside as a minor inconvience. Still it begs the question why is an undefined offset a warning and not a fatal in php? im sure most other languages generate a compile error (or is that writing past the end of an array, bah)

and i havnt personally used the shuffle() function before i havnt really had mush need for shuffling an array before so it hasnt come to my attention yet, so there isnt any 'harm' in it in fact its probably quicker (as its a language function therefore written and compiled in C and doesn't need to go through the interpreter)

bah i should actually be working right now, so i guess ill get on with it before i get sacked.

Orc 05-07-2008 12:17 PM

Quote:

Originally Posted by sketchMedia (Post 14402)
yea thanks your correct Salathe i overlooked that, infact i didnt think it through properly (as i previously said) tend to loose track of index offsets for some reason, i guess my brain just brushes it aside as a minor inconvience. Still it begs the question why is an undefined offset a warning and not a fatal in php? im sure most other languages generate a compile error (or is that writing past the end of an array, bah)

and i havnt personally used the shuffle() function before i havnt really had mush need for shuffling an array before so it hasnt come to my attention yet, so there isnt any 'harm' in it in fact its probably quicker (as its a language function therefore written and compiled in C and doesn't need to go through the interpreter)

bah i should actually be working right now, so i guess ill get on with it before i get sacked.

Well since the way I was thinknig would work, this thread was for nothing, cause I thought of that first xD. oh well, im going to go rest ( gta 4 mp ) and then go to sleep


All times are GMT. The time now is 01:46 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0