Results 1 to 2 of 2

Thread: Random number

  1. #1
    Join Date
    Sep 2016
    Location
    Netherlands
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Random number

    Hello everyone,

    I've used this example to display a random link. It works, but sometimes it displays the same link two times after each other. How can I block it form displaying the same link twice after each other? So that it may only show again after - let say - three other links?

    I hope there is a solution!

    Greetings,

    Erik

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript">
    (function(w){
    	var links = [];
    	links[0] = "http://www.javascriptkit.com";
    	links[1] = "http://www.dynamicdrive.com";
    	links[2] = "http://www.cssdrive.com";
    	links[3] = "http://www.codingforums.com";
    	links[4] = "http://www.news.com";
    	links[5] = "http://www.gamespot.com";
    	links[6] = "http://www.msnbc.com";
    	links[7] = "http://www.cnn.com";
    	links[8] = "http://news.bbc.co.uk";
    	links[9] = "http://www.news.com.au";
    
    	var cook = {
    		set: function(n, v, d){ // cook.set takes (name, value, optional_persist_days) - defaults to session if no days specified
    			if(d){var dt = new Date(); 
    				dt.setDate(dt.getDate() + d);
    			d = '; expires=' + dt.toUTCString();}
    			document.cookie = n + '=' + escape(v) + (d || '') + '; path=/';
    		},
    		get: function(n){ // cook.get takes (name)
    			var c = document.cookie.match('(^|;)\x20*' + n + '=([^;]*)');
    			return c? unescape(c[2]) : null;
    		}
    	};
    
    	var arrayindex = (typeof [].indexOf === 'function')? function(ar, item){return ar.indexOf(item);} :
    		function(ar, item){
    			var idx = ar.length;
    			while(--idx > -1){
    				if(ar[idx] === item){
    					return idx;
    				}
    			}
    			return -1;
    		};
    
    	w.randomlinks = function (){
    		var len = links.length, myrandom = Math.floor(Math.random() * len ), stored = cook.get('storedrandompagenums');
    		if(stored){
    			stored = stored.split('.');
    			while(arrayindex(stored, myrandom.toString(10)) > -1){
    				myrandom = (myrandom + 1) % len;
    			}
    			stored.unshift(myrandom);
    			if(stored.length > 3){
    				stored.pop();
    			}
    			stored = stored.join('.');
    		} else {
    			stored = myrandom;
    		}
    		cook.set('storedrandompagenums', stored);
    		w.location.href = links[myrandom];
    	}
    })(window);
    </script>
    <form>
    <input type="button" value="random link!" onclick="randomlinks();">
    </form>
    NOTES: You no longer need to deal with the number of entries in the links array, the code will calculate it for you. Just add or remove as necessary, just make sure there are more than 3 and that they are numbered sequentially starting at 0. The non-repeating nature of the code (the three last pages will not repeat) relies upon the user having cookies turned on, most do. Any questions, feel free to ask.
    Last edited by jscheuer1; 09-30-2016 at 10:02 AM. Reason: minor code improvements, add notes
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Random Number Generator
    By Jay Dog in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 07-08-2015, 10:49 PM
  2. Random Number
    By Rockonmetal in forum PHP
    Replies: 5
    Last Post: 09-16-2007, 09:30 PM
  3. Random number combined with switch does not work
    By rctxtreme in forum JavaScript
    Replies: 10
    Last Post: 04-17-2007, 05:51 PM
  4. Random number addition script - help!
    By Shenshen in forum JavaScript
    Replies: 1
    Last Post: 05-23-2006, 06:17 PM
  5. Help - Random Number script..
    By kamisaibou in forum JavaScript
    Replies: 2
    Last Post: 03-02-2006, 10:46 PM

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
  •