Click to See Complete Forum and Search --> : Randomization


leia18
02-25-2003, 01:11 PM
I don't really know js persay, but I've modified scripts before. The problem is that I'm looking for something that will return one word or phrase from a list of thirty, and will also return a random number from 1-100 on page load.

It seems rather simple, but I'm just running into the wall on it. Can someone help me out?

AdamBrill
02-25-2003, 01:18 PM
This will create a random number between 1 and 100:

Math.floor(Math.random()*100)

to return a word or phrase, just put all of the words/phrases into an array and call a random one between 1 and 100. It would look something like this:

Quotes = new Array("First Quote",
"Second Quote",
"Third Quote",
"Fourth Quote");

document.write(Quotes[Math.floor(Math.random()*Quotes.length));

I hope that helps. :)

Charles
02-25-2003, 01:33 PM
<script type="text/javascript">
<!--
String.getRandomWord = function () {var a = ['Robreus', 'ismoprat', 'icrod', 'bopaschius', 'fin', 'lobrue', 'equidasnanis', 'Eveps', 'snan', 'snipomon', 'ori', 'schesestotis', 'ic', 'aspaps', 'Phipismius', 'ebribrius', 'pu', 'remodra', 'quidulen', 'Cicr', 'vocotis', 'teth', 'drogopam', 'hem', 'iquidibriant', 'ninenat', 'Nutriem', 'barupo', 'itirh', 'stimuciem', 'pso', 'snagoc', 'orhibront', 'Plinatheus', 'pragibres', 'im', 'setore', 'promodotis', 'Larh', 'snisc', 'inequada', 'bre', 'vanotis', 'ceh', 'oleb', 'Iscen', 'opuna', 'emipr', 'phiporit', 'fe', 'hosnes', 'drovoner', 'Asidr', 'isapri', 'equidiprat', 'et', 'craps', 'girhi', 'tiquiduer']; return a[Math.floor(Math.random() * a.length)] }

document.write('<p>', String.getRandomWord(), '</p>');
document.write('<p>',Math.random() * 100, '</p>');
// -->
</script>

It's important to keep in mind, though, that as JavaScript will fail at least one in ten times (http://www.thecounter.com/stats/2002/November/javas.php) you cannot rely upon it. Do not use JavaScript for anything but fluff and make sure that your page still works when you turn the JavaScript off.

leia18
02-25-2003, 02:28 PM
Thank you both for your help- I have things running just like I wanted. ^-^