Advanced Search

Results 1 to 6 of 6

Thread: Randomizer

  1. #1
    Join Date
    Feb 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Randomizer

    Searched the forums and the site to "randomize" a set of pictures, have one picture change everytime the page is hit

    Thank you!

  2. #2
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Something like this? http://www.jsmadeeasy.com/javascript...s/template.htm

    Otherwise, try www.google.com .

    cr3ative
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  3. #3
    Join Date
    Dec 2004
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Code:
    <script language='javascript'>
    
      //-- Generate a random number between 0 and max.
      function randomNum(max) {
        var rNum=NaN
        while (isNaN(rNum)) {
          rNum=Math.floor(Math.random()*(max))
        }
        return rNum
      }
      var pic = new Array()
      pic[0]="shot01.jpg"
      pic[1]="shot02.jpg"
      pic[2]="shot03.jpg"
      pic[3]="shot04.jpg"
      pic[4]="shot05.jpg"
      pic[5]="shot06.jpg"
      pic[6]="shot07.jpg"
      pic[7]="shot08.jpg"
      pic[8]="shot09.jpg"
      pic[9]="shot10.jpg"
      pic[10]="shot11.jpg"
      pic[11]="shot12.jpg"
      pic[12]="shot13.jpg"
      pic[13]="shot14.jpg"
      pic[14]="shot15.jpg"
    </script>
    just rename the shotx.jpg

    then
    Code:
    <script language='javascript'>
                   imgTag='<img border="0" src="'
                   imgTag+=pic[randomNum(pic.length)]
                   imgTag+='" align="left" hspace="0"'
                   imgTag+=' width="200" height="150"'
                   document.write (imgTag)
    		</script>
    for the pic
    change the align, width and all that crap. this should work, it works for me, tweak it if you have to.

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by darco9x2
    Code:
    <script language='javascript'>
    Please use up-to-date mark-up. Use of the language attribute should have ended over six years ago.

    HTML Code:
    <script type="text/javascript">
    is sufficient in all cases.

    Code:
        while (isNaN(rNum)) {
          rNum=Math.floor(Math.random()*(max))
        }
    I'm curious: why do you think that will produce NaN? Unless the argument max is not a number, I don't see how that could happen. However, even then the code will go into an infinite loop so it should be the responsibility of the caller to make sure the argument is valid.

    Code:
    /* Generates a pseudo-random integer
     * in the range o <= x < (n + o)
     * where o defaults to zero (0).
     */
    function random(n, o) {o = +o || 0;
      return Math.floor((Math.random() % 1) * n) + o;
    }
    Code:
      var pic = new Array()
      [...]
    It's generally better to use an array literal to create an initialised array:

    Code:
    var pic = [
      'shot01.jpeg',
      'shot02.jpeg',
      /* ... */
      'shot15.jpeg'
    ];
    Code:
    imgTag='<img border="0"
    If you don't want borders on images, use CSS:

    Code:
    img {
      border-style: none;
    }
    It's far more efficient. Dump deprecated attributes like align and hspace. HTML 3.2 is dead.

    Mike

  5. #5
    Join Date
    Dec 2004
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    k thanks

  6. #6
    Join Date
    Jul 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb it also works without an array ...

    Hi everybody,
    just found your thread and noticed that all of you got stuck on the idea of using an array for picking a random picture. Here is a (shorter) solution without having to set up and fill an array:

    Code:
    <script type="text/javascript">
    function randpic(pic,n) {
      // picks a random picture
      // pic: path to pictures and first (common) part of picture name
      // each picture carries a 3-digit number from 1 to n and ends with ".jpg"
      var i=String(1000+Math.ceil(Math.random()*n)).substr(1)
      document.writeln("<img src=\""+pic+i+".jpg\" alt=\"Bild Nr. "+i+"\">")
    }
    </script>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •