Random Generator... How can I make one?

Rad

...
Reaction score
228
I remember back 4+ months ago, one of my friends made a sweet randomizer that took a few strings from the database and combined them. It was like this:

Code:
[b]Weapon[/b].....[b]Effect[/b].....[b]Other[/b]
Dagger.....Quick......of the Eagle
Sword......Strong.....of the Bear
Axe........Large......<null>

You couldnt see the list, but you hit the 'Create' button and it takes random strings from above and put then together like "Strong Dagger of the Bear" or "Large Axe of the Eagle"... Sometimes they did sound really stupid, but I would like to know how you can do this with HTML, PHP... anything. Something easy as I only have experience with HTML.

Is there any programs or anything? Just something I would like to mess around with :D
 

Rad

...
Reaction score
228
Not knowing any PHP I have no idea what to fill in... Is there an example? I Looked at the page and well... it doesnt tell me much.

PHP:
Example 1. array_rand() example
<?php
srand((float) microtime() * 10000000);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>

See also shuffle().

It doesnt say what to fill in... Do you replace Neo, Morpgeus, Trinity etc? I dont get it...

-----

I want something that clerifies where to put new words, how to make 3 sections, takes 1 random word from each section and shows the result. But I dont know how to make it randomize when I push a button...

Actually Ill look at dreamweaver and see if it can help.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
It's clearer, maybe, with several:

$begin = array("The", "A", "One"); // Change this
$middle = array("new","old","previous"); // and this
$end = array("car","house","tv"); // and this

echo $begin[array_rand($begin)] . $middle[array_rand($middle)] .$end[array_rand($end)]. "\n"; // don't touch this


Or, if PHP isn't available, JavaScript will do:

<script type="text/javascript">
words = new Array("The","A","One");
document.write(words[Math.round(Math.random() * (words.length - 1))]);
</script>
 

Rad

...
Reaction score
228
PHP:
<?php
$begin = array("The", "A", "One"); // Change this
$middle = array("new","old","previous"); // and this
$end = array("car","house","tv"); // and this

echo $begin[array_rand($begin)] . $middle[array_rand($middle)] .$end[array_rand($end)]. "\n"; // don't touch this
?>

Can I just put this into a blank document.php? Im trying to get it to work with dreamweaver... but dreamweaver sucks :p

Can you make me an example? Ive never made my own javascript/php script... only a few web pages with HTML.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
FWIW, here's a "complete" html page:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Rad's random garbage</title>
<link rev="made" href="AceHart">
<meta name="Author" lang="en" content="AceHart">
<meta name="keywords" lang="en" content="random,test,demo,rad">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var FirstPart = new Array("The", "A", "One"); // Change this
var MiddlePart = new Array("new","old","previous"); // and this
var EndPart = new Array("car","house","tv"); // and this
</script>
</head>
<body>

<p>
Hit refresh.
</p>
<p>
<script type="text/javascript">
document.write(FirstPart[Math.round(Math.random() * (FirstPart.length - 1))] + ' ' + MiddlePart[Math.round(Math.random() * (MiddlePart.length - 1))] + ' ' + EndPart[Math.round(Math.random() * (EndPart.length - 1))]); // Don't touch this
</script>
</p>

</body>
</html>

It's even valid Html 4.01 strict... :D
 

Rad

...
Reaction score
228
Ohh thats cool!

Is there an easy way to make it generate say... 5 different ones at once? Do I just repeat the code or is there some type of variable with PHP...

EDIT~Nevermind I found you just copy paste...

Code:
<script type="text/javascript">
document.write(FirstPart[Math.round(Math.random() * (FirstPart.length - 1))] + ' ' + MiddlePart[Math.round(Math.random() * (MiddlePart.length - 1))] + ' ' + 

EndPart[Math.round(Math.random() * (EndPart.length - 1))]); // Don't touch this
</script>
and it works fine. Thanks a bunch Ill have to add to this and impliment it to my site. (If anyone ever still goes there you can tell I havent been very up-to-date on it anyways. That will change)


EDIT #2~ Is there a way to make it 'refresh' only the words when I click a button? Maybe a short example if its easy... if you think it will be to hard for me just let me know because I dont like getting all confused and stuff.

EDIT #3~ What does FWIW, AFAIK and all those other acronyms mean?
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
If you really wanted, you could use some DOM methods to directly alter the nodes...


For really simple, add this:

<form action="http://your domain here/page name.htm">
<p>
<input type="button" onClick="window.location.reload()" value="Refresh">
</p>
</form>
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top