Development > Feature Requests

a random word generator block :-)

(1/4) > >>

Cat McFarlane:
Hi, was advised about some code for a word generator, which people on my art forum love as drawing inspiration, and which I can base art games around. Have put it into an html simpleportal block. This is the text (519 words entered alphabetically, so far). Issue I have is that often the words are coming up in alphabetical order rather than really randomly, and I want to be sure the art games don't get stuck on a loop, with everyone only getting early words and the same alphabetical words etc.

Also, the box shown in pic comes up after a few clicks, and I don't want people clicking on it, as that stops the generator working ... not sure if only I can see that, but just to be safe.

Thanks

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Random thingy</title>

<script language type="text/javascript">
<!--

function getMessage()
{
var ar = new Array(519)
ar[0] = ... 519 alphabetically here


var now = new Date()
var sec = now.getSeconds()
alert("Random thingies:\n\n" + ar[sec % 519])
}

//-->
</script>

</head>

<form>
<input type="button" name="again" value="Generate another"

onClick="getMessage()">
</form>

</body>
</html>

EDIT: tried incorporating the following instead ...

<script language="JavaScript">
<!--
var randomString = new Array ();
randomString[0] = "a random string";
randomString[1] = "another random string";
randomString[2] = "another random string";
randomString[3] = "another random string";
randomString[4] = "another random string";
randomString[5] = "another random string";
randomString[6] = "another random string";
randomString[7] = "another random string";
var i = Math.floor(7*Math.random())

document.write(randomString);
//-->
</script>

but same alphabetized results, not random across entire 519 words.

Had put at bottom of list ... var i = Math.floor((Math.random()*519)+1);

Chen Zhen:
Here is a javacript version of what was requested:


--- Code: ---<script language type="text/javascript"><!--
function getMessage()
{
var ar = new Array(
"test1",
"test2",
"test3",
"test4",
"test5");
alert("Random thingies:\n\n" + ar[Math.floor(Math.random()*(ar.length))]);
}
//--></script>
<form>
<input type="button" name="again" value="Generate another" onclick="getMessage()" />
</form>

--- End code ---


Using PHP, you might try something like this:


--- Code: ---$ar = array(
"test1",
"test2",
"test3",
"test4",
"test5");

echo '
<form action="',currentPageUrl(),'" method="post">
<input type="submit" id="wordgenerator" name="wordgen" value="Generate another" />
<input type="hidden" name="submit_word" />
</form>';

if(isset($_REQUEST['wordgen']) && isset($_REQUEST['submit_word']))
echo '<br />Random thingies:<br /><br /> ' . $ar[mt_rand(0, (count($ar)-1))];

function currentPageUrl($pageURL = 'http://')
{           
if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";   
   
if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
   
return $pageURL;


--- End code ---

Cat McFarlane:

--- Quote from: Underdog on September 23, 2013, 11:42:57 PM ---Here is a javacript version of what was requested:


--- Code: ---<script language type="text/javascript"><!--
function getMessage()
{
var ar = new Array(
"test1",
"test2",
"test3",
"test4",
"test5");
alert("Random thingies:\n\n" + ar[Math.floor(Math.random()*(ar.length))]);
}
//--></script>
<form>
<input type="button" name="again" value="Generate another" onclick="getMessage()" />
</form>

--- End code ---


Using PHP, you might try something like this:


--- Code: ---$ar = array(
"test1",
"test2",
"test3",
"test4",
"test5");

echo '
<form action="',currentPageUrl(),'" method="post">
<input type="submit" id="wordgenerator" name="wordgen" value="Generate another" />
<input type="hidden" name="submit_word" />
</form>';

if(isset($_REQUEST['wordgen']) && isset($_REQUEST['submit_word']))
echo '<br />Random thingies:<br /><br /> ' . $ar[mt_rand(0, (count($ar)-1))];

function currentPageUrl($pageURL = 'http://')
{           
if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";   
   
if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
   
return $pageURL;


--- End code ---

--- End quote ---

Thanks so much, Underdog.   :)  I've got the block set as a left block, and have noticed that some changes made in other boxes on the portal page cause changes in other boxes, so am wondering if it could be that, as the first code meant that when I clicked on the button there wasn't a word generated (did I need to number each word?), and the second cut off all boxes on the rest of the page underneath that word, and seemed to be reading off the other word list, even though that wasn't active on the page at the time. And logo.jpg I added at top of page, in theme (Fresh Looks) keeps going back to tiny version/have to re-upload. 519 words, and just shows early ones. 
Thanks, Cat

ccbtimewiz:
Can you show me a live demo of the problem?

Chen Zhen:

  Here is another PHP example that will not pick the same choice twice (until the loop is reset). Replace the array text to your own entries and change the echo to how you wish it to display.


--- Code: ---$ar = array(
"test1",
"test2",
"test3",
"test4",
"test5"
);

echo '
<form action="', currentPageUrl(), '" method="post">
<input type="submit" id="wordgenerator" name="wordgen" value="Generate another" />
<input type="hidden" name="submit_word" />
</form>';

if(isset($_REQUEST['wordgen']) && isset($_REQUEST['submit_word']))
{
shuffle($ar);
if (empty($_SESSION['wordgenerator']) ? 0 : $_SESSION['wordgenerator'] >= count($ar))
{
$_SESSION['wordgenerator'] = 0;
foreach ($ar as $word)
$_SESSION['wordgen'][$word] = false;
}

foreach ($ar as $generated_word)
{
if (empty($_SESSION['wordgen'][$generated_word]))
{
$_SESSION['wordgen'][$generated_word] = true;
$_SESSION['wordgenerator'] = !empty($_SESSION['wordgenerator']) ? $_SESSION['wordgenerator'] + 1 : 1;
echo '<br />Random thingies:<br /><br /> ' . $generated_word;
break;
}
}
}

function currentPageUrl($pageURL = 'http://')
{
if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";

if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

return $pageURL;
}

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version