Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Return 2

Status
Not open for further replies.

MarkZK

Technical User
Jul 13, 2006
202
GB
Hi, all

I wonder if anybody could please explain this to me, I've stripped down the code to its basic functions to try and understand is myself, but, I still don't.

I'm trying to get the second option (which matches the second Regexp as I want it to) to return a random reply (1 of 3), but the random reply is not so random, every time I press test is returns the same string unless I refresh the browser (which I don't want to do). Do I need to write the random reply function differently or is it something else ?.

the sample code is,
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>

<script type="text/javascript">
//<![CDATA[
var search_text=new Array();
var find_reply=new Array();
var found_res=new Array();
//
search_text[0]=/test1/i;
search_text[1]=/another test/i;
//
find_reply[0]="this is test 1";
find_reply[1]=rand_reply();
//
function _example(s){
var str;
for(var j=0;j<search_text.length;j++){
  if(s.search(search_text[j])>-1){
 str=find_reply[j];
         alert(str);//alert out come
          }
          }
}
//
function rand_reply(){
     found_res[0]="alert this";
     found_res[1]="or alert this one";
     found_res[2]="or, finally this";   
  return found_res[rdm(0,2)];
}
//
function rdm(x,y){return Math.round(Math.random()*(y-x))+x;}
//]]>
</script>
<title></title>
</head>
<body>
<form action="null" onsubmit="_example(document.getElementById('sample').value);return false;">
<select id="sample">
<option value="test1">test1</option>
<option value="another test">another test</option>
</select> <input type="submit" name="submit" value="test" /></form>
</body>
</html>

Thanks for reading.
 
Try something like
Code:
return found_res[Math.floor(Math.random() * found_res.length)];

Lee
 
Thanks for the quick response Trollacious, just did a quick check and it seems to do the same thing that way too.

Thanks again.
 
I tried this
Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<SCRIPT language="javascript">
var found_res = new Array();

function rand_reply(){
     found_res[0]="alert this";
     found_res[1]="or alert this one";
     found_res[2]="or, finally this";
  alert(found_res[Math.floor(Math.random() * found_res.length)]);
}

</script>
<form>
<input type="button" value="Click Me" onclick="rand_reply();">
</form>

</body>
</html>

and got a variety of return values. You only have 3 elements in the array you're using, so there's a pretty good possibility of the same random number coming up several times in a row. The more elements in the array, the more likely you'll get a different one displayed each time.

Lee
 
Thanks, actually that does work, so I recheck it your way with the other random function....

Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<SCRIPT language="javascript">
var found_res = new Array();

function rand_reply(){
     found_res[0]="alert this";
     found_res[1]="or alert this one";
     found_res[2]="or, finally this";
  //alert(found_res[Math.floor(Math.random() * found_res.length)]);
alert(found_res[rdm(0,2)]);
}
function rdm(x,y){return Math.round(Math.random()*(y-x))+x;}
</script>
<form>
<input type="button" value="Click Me" onclick="rand_reply();">
</form>
</body>
</html>

and that also works, so it must be something to do with the regexp that is making it freeze to one reply, I really need to keep the regular expressions, but you've help me get one step closer, thanks.

 
[tt]find_reply[0]="this is test 1";
[red]//comment find_reply[1] out, so that it won't be fixed once for all
//find_reply[1]=rand_reply();[/red]
//
function _example(s){
var str;
[blue]//move find_reply[1] to within the function
find_reply[1]=rand_reply();[/blue]
for(var j=0;j<search_text.length;j++){
if(s.search(search_text[j])>-1){
str=find_reply[j];
alert(str);//alert out come
}
}
}
[/tt]
 
of course, I was making it a global variable, I spent ages thinking about this (almost dreamt about it) and you came along and spotted it straight away :)



Thank you Tsuji.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top