SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Feb 27, 2001, 16:40 #1
- Join Date
- Nov 2000
- Location
- Ont, Canada
- Posts
- 234
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've created an array with 5 occurrences. Before processing the array I want to set an index variable to a random value between 0 and 4. I want to do this so I don't always get the same banner starting from a Ad Rotator script when the page is refreshed. Any ideas how I can set the index variable to a random value?
Brian Smith
www.mylittlechampion.com
-
Mar 2, 2001, 15:38 #2
- Join Date
- Sep 2000
- Location
- United States
- Posts
- 1,921
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I believe you can use the Rnd() Function for numbers, but am unsure of it's exact syntax. You'd probably want to look it up on www.4GuysFromRolla.com or www.ASP101.com.
Hope this helps!
-
Mar 2, 2001, 22:21 #3
- Join Date
- Nov 2000
- Location
- Ont, Canada
- Posts
- 234
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks Sean. I've used the following and it works fine.
<SCRIPT language="JavaScript"><!--
var maxNumber = 1000;
var randomNumber = Math.round(maxNumber * Math.random());
//--></SCRIPT>
I got it from Cnet at
http://www.builder.com/Programming/S...dir1.Stupid4_6Brian Smith
www.mylittlechampion.com
-
Mar 3, 2001, 10:29 #4
-
Mar 4, 2001, 21:14 #5
- Join Date
- Jul 1999
- Location
- SC, USA
- Posts
- 390
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hey guys,
Just as a note (not very important), but in JavaScript, when you create a random number, you should always "floor it" to create something truly random, rather than round it. Example:
Code:<SCRIPT language="JavaScript"><!-- var maxNumber = 1000; var randomNumber = Math.floor(maxNumber * Math.random()); //--></SCRIPT>
http://javascriptcity.com/forums/sho...?threadid=2366
aDogModerator at www.javascriptcity.com/forums/
Bookmarks