...

View Full Version : Randomizing



Spency
11-23-2012, 10:35 AM
Hello, I'm having issues trying to make an alphabetic randomizer... specifically, it needs to only randomize the order of 2 letters each time the page is reloaded.

Here is the code I currently have in place, but when I try and load it in my browser I get a blank screen.


<script type="text/javascript">
//<![CDATA[
<!--
Long.toHexString(Double.doubleToLongBits(Math.random()));

//

//-->
//]]>
</script>

This is the code I'm using for it, but it won't show up at all! I hijacked the code from here: http://mynotes.wordpress.com/2009/07/23/java-generating-random-string/

Thank you in advance! Would it help if I posted my whole HTML file? It's not very big but it might help.

Spency
11-23-2012, 12:16 PM
Ok now I've decided to use a modified version of vwphillips's code from http://www.codingforums.com/archive/index.php/t-89056.html


<html><head><title>Random Order</title>

<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/


var formAry=['Z','Y']
var scaleAry=['Y','Z']

var TO;

function Notes(id,spd){
var table=document.getElementById(id);
var cells=table.getElementsByTagName('TD');
table.style.fontSize='20px';
clearTimeout(TO);
NotesDo(cells,spd);
}

function NotesDo(cells,spd){
cells[2].innerHTML=formAry[Math.floor(Math.random()*formAry.length)];
cells[3].innerHTML=scaleAry[Math.floor(Math.random()*scaleAry.length)];
TO=setTimeout(function(){ NotesDo(cells,spd); },spd);
}

/*]]>*/
</script></head>

<body>
<table id="notes" cellpadding="0" cellspacing="0" border="1" >
<tr>
<td width=100 align=center >First Test</td>
<td width=100 align=center >Second Test</td>
</tr>
<tr >
<td width=100 align=center >Z, Y</td>
<td width=100 align=center >Y, Z</td>
</tr>
</table>
<input type="button" value="Randomize" onclick="Notes('notes',10000);" />
</body>

</html>

Is there a way to make it so that the same characters do not show up in both cells? I tried removing one of the cells, but every time I did anything to modify the B cells I broke the randomizer completely.

Tips?

Philip M
11-23-2012, 01:08 PM
[QUOTE=Spency;1294201]Hello, I'm having issues trying to make an alphabetic randomizer... specifically, it needs to only randomize the order of 2 letters each time the page is reloaded.

Here is the code I currently have in place, but when I try and load it in my browser I get a blank screen.


<script type="text/javascript">
//<![CDATA[
<!--
Long.toHexString(Double.doubleToLongBits(Math.random()));

//

//-->
//]]>
</script>

That code is JAVA, not Javascript. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

If all you want to do is randomize the order of two letters on page load, then



<script type = "text/javascript">

var twoletters = "XY"
var now = new Date().getSeconds(); // returns 0-59
if (now%2 == 0) { // even number
twoletters = "YX";
}
alert (twoletters);

</script>



Quizmaster: The small primates called lemurs are native to which large island off the coast of Africa?
Contestant: Argentina.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum