Jump to content


Function Randomizer


3 replies to this topic

#1 Raldo

    Young Padawan

  • Members
  • Pip
  • 5 posts

Posted 13 August 2006 - 09:10 PM

I was wondering if it were possible to make an array of functions.
For instance, like a quote generator has an array of quotes and randomly picks one to display via a function. Is there a way to make an array of similar functions?

I need it for a random sentance generator, where I have multiple arrays of words to randomly display. I'm looking for an array that would randomly choose which array of words to display. Make sense?

My next hurdle is this: How do I randomize the length of the sentance?

#2 NGPixel

    Senior Programmer

  • P2L Staff
  • PipPipPipPip
  • 1,410 posts
  • Gender:Male
  • Location:Montreal, Canada
  • Interests:Web Design : Coding : Animation

Posted 13 August 2006 - 10:34 PM

your "array of functions" don't make sense.

But you can have an array in an array

for example, you could have an array of 10 keys, each values would be another array containing the words.

Then you could choose a random number that correspond to the first key.

$var[1][1] = "word1";
$var[1][2] = "word2";
$var[1][3] = "word3";
$var[2][1] = "word1";
$var[2][2] = "word2";
etc.

You normally do this automatically from a database.

---------

Not sure why you want to choose a random sentence length, but using the rand() function, you can do this. Then using substr() or directly using the loop with the generated length.

Let me know if you need more help :)

#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 August 2006 - 02:13 AM

http://php.net/manua...e-functions.php

The above could help answer the array of functions problem. But i would need a better description/example of the problem before i can offer better advice.

#4 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 15 August 2006 - 04:11 PM

View PostRaldo, on Aug 14 2006, 02:10 AM, said:

I was wondering if it were possible to make an array of functions.
For instance, like a quote generator has an array of quotes and randomly picks one to display via a function. Is there a way to make an array of similar functions?

I need it for a random sentance generator, where I have multiple arrays of words to randomly display. I'm looking for an array that would randomly choose which array of words to display. Make sense?

My next hurdle is this: How do I randomize the length of the sentance?

if you wanted to pull it from a SQL database, I know you can do it real easily like this.

SELECT * quotes LIMIT RAND(),1






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users