Shoutbox

Random Word [site] - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: General (/forumdisplay.php?fid=11)
+---- Forum: General Chit Chat (/forumdisplay.php?fid=14)
+----- Thread: Random Word [site] (/showthread.php?tid=73033)

Random Word [site] by Eddie on 03-26-2007 at 08:22 AM

Hello, on my site, im planning to host a competition where someone must find a page and submit the code on the page, is there any way in PHP or JavaScript that will allow for a random word to be placed on the page somewhere? :)


RE: Random Word [site] by the DtTvB on 03-26-2007 at 09:09 AM

Yeah, you just need a dictionary.

I recommend using english-words.50 file from scowl-6.

Now you have to create an array from a file and pick a word.

Example code:

code:
<?php

$wordlist = file('english-words.50');

function pick_a_word() {
    global $wordlist;
    return trim($wordlist[rand(0, count($wordlist) - 1)]);
}

for ($i = 0; $i < 20; $i ++)
    echo pick_a_word() . ' ';

?>

RE: Random Word [site] by Eddie on 03-26-2007 at 09:11 AM

quote:
Originally posted by the DtTvB
Yeah, you just need a dictionary.

I recommend using english-words.50 file from scowl-6.

Now you have to create an array from a file and pick a word.
Will the word just automatically come up? and im not using words, just random codes *-)
RE: Random Word [site] by the DtTvB on 03-26-2007 at 09:17 AM

You just have to create a list of characters [1] [2] and select it randomly.

code:
<?php

$charlist = (array_merge(
    range('0', '9'),
    range('a', 'z'),
    range('A', 'Z')
));

function pick_a_character() {
    global $charlist;
    return $charlist[rand(0, count($charlist) - 1)];
}

function create_code($length) {
    $o = '';
    for ($i = 0; $i < $length; $i ++)
        $o .= pick_a_character();
    return $o;
}

$code = create_code(12);
echo "The code is $code";

?>

RE: Random Word [site] by Felu on 03-26-2007 at 09:18 AM

As simple as
1. Create and array of words/codes
2. Loop through the array and pick up a random word/code.

I'll post the code later. I have to go atm.


RE: Random Word [site] by Eddie on 03-26-2007 at 10:00 AM

Ok that worked well, but i want to be able to choose wot the codes are :P
Oh and around 7 word codes only :P Sorry to be a bother :( [EDIT: Figured This Bit Out]

I know this makes it a little less random to the developer, but to the general public, its still random :P

[EDIT: Figured it out :) Thanks guys]


RE: Random Word [site] by Volv on 03-26-2007 at 12:13 PM

code:
<?php
$words = array('word1', 'word2', 'word3'); //Fill an array with the random code words

$randomword = $words[rand(0, count($words)-1)]; //Pick a random code word from the array

echo $randomword; //Output the random code word to the page
?>


EDIT: Only noticed the EDIT in Eddie's above post just now, but I'll leave this post here in case anyone needs it for reference or something :p