Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Random Number in a Javascript call.

Posted on 2004-10-26
Medium Priority
295 Views
In an asp page I am making a javascript call, one of the parameters is called 'first'. I want to stick a random number in there from 1-10. What's the best way to do this?
Here is the javascript:

So where first=3, it should be first=(1-10).

Thanks!
0
Question by:seahawk9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 4

LVL 2

Expert Comment

ID: 12415752

// Math.random() returns a decimal number between
// 0 and 1.  You multiple the result by the range
// of numbers you want.

// This will generate 0-10.
var rannum = 0
while(rannum == 0) {
rannum = Math.random()*10;
}

// This would generate 145-200
// rannum = Math.random()*55 + 145;

// rannum is now a decimal value, to turn it into
// an integer use Math.round to round it to the nearest
// integer.  Math.floor(rannum) to always round down and
// Math.ceil(rannum) to round up.

rannum = Math.round(rannum);

location.href = "http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=" + rannum

location.href will redirect you to the url specified
Hope this helps
0

Author Comment

ID: 12415796
Can you give me the exact syntax? It looks like your solution is in javascript not asp. So how would it look?
Would is be something like this?:

<SCRIPT language="JavaScript"

// This will generate 0-10.
var rannum = 0
while(rannum == 0) {
rannum = Math.random()*10;
}

// This would generate 145-200
// rannum = Math.random()*55 + 145;

// rannum is now a decimal value, to turn it into
// an integer use Math.round to round it to the nearest
// integer.  Math.floor(rannum) to always round down and
// Math.ceil(rannum) to round up.

rannum = Math.round(rannum);

location.href = "http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=" + rannum

SRC=location.href></SCRIPT>
0

Author Comment

ID: 12415806
FYI, that gives me back a blank result.
0

LVL 2

Expert Comment

ID: 12416041
This is all you need

Take out the src=

just try the following below

------------------------------------------------------------
<SCRIPT language="JavaScript">
var rannum = 0
rannum = Math.random()*10;
rannum = Math.ceil(rannum);

location.href = "http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=" + rannum;
</SCRIPT>
0

Author Comment

ID: 12416078
Thanks, but that totally moves me off my asp page. I want the SRC embedded in my page, not taking control of the page.
0

LVL 2

Expert Comment

ID: 12416103
I would just use vbScript to generate the random number.  I'm not quite sure why you are using "src" in the javascript or what "src" is used for.

If you could elaberate on this a little more that would be great.

Thanks,
0

Author Comment

ID: 12416175
Here is the code on a broader scale.
<% If irsSearchCount Mod 5 = 0 Then %>
<tr>
<td ALIGN="CENTER" COLSPAN="7" BGCOLOR="#FFFFFF"><font FACE="HELVETICA" SIZE="-1">
<hr WIDTH="80%">
<hr WIDTH="80%">
</td>
</tr>
<%
End If
%>

I have tried doing this:

And I get a type mismatch error.
0

LVL 2

Expert Comment

ID: 12416314
You can't use asp inside of javascript.

The only reason to use a "src" attribute in a "<script>" tag is to specify an exteral javascript file that you would like to use instead of typeing the javascript in the page itself.  Is that what you are trying to do with the cgi file.

Usually you would use a .js file.

0

Author Comment

ID: 12416346
In the documentation there is a parameter called 'first'
by manually changing that we get a variety of ads coming back. You can see this yourself in a browser:

Copy and paste the whole thing in there and change the value for first from 3 to another number and you will see the ad change.

I am trying to pull that 468x60 ad back into my asp page. That's why I am using a javascript call with the src attribute. I admit to not knowing very much about javascript. But it works in the code with the first parameter hard coded, I just want to vary that each time I go and get it again. It doesn't even matter to me if it is 1,2,3,4,5. It just needs to be different every five ads, thus tying it to the mod= 5 portion of the code.

0

LVL 13

Accepted Solution

nurbek earned 2000 total points
ID: 12418986
you can use the asp variables in javascript
type mismatch error is
you forgot "=" sign

you can use asp generated codes in your banner

also
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is: Â  The Exchange of informatioâ€¦
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interactâ€¦
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrasâ€¦
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. â€¦
###### Suggested Courses
Course of the Month8 days, 8 hours left to enroll