![]() |
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 |
not very pretty, took about 1min to knock up xD so theres most likely to be mistakes or easier ways:
PHP Code:
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:
|
Quote:
|
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? |
Quote:
but cant you just use array_unqiue for making them unique words aswell? |
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?
|
Quote:
|
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
|
Quote:
|
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:
|
Quote:
|
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. |
Quote:
|
| 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