SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist jor133d's Avatar
    Join Date
    Jul 2003
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random Numbers, Not Consecutive

    Hi,

    I am looking for some assistance with modifying some javascript code I found. Currently the code produces 2 different random numbers but I need three.

    For example, it should give me a readout like 0, 2, 1 but NOT 0, 2, 0 or 1,1,2.

    Can anyone help me?

    Code:
    <script language="JavaScript">
    <!-- Hide this script from old browsers --
    
    var randomNum1=Math.floor(Math.random()*2);
    var randomNum2=randomNum1;
    while (randomNum2 == randomNum1){
    randomNum2=Math.floor(Math.random()*2);
    }
    
    alert(randomNum1 + ' and ' + randomNum2);
    </script>

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Populate an array with consecutive numbers then sort it randomly.
    Code:
    var numbers = [1, 2, 3];
    numbers.sort(function(){return Math.random() - 0.5;});
    alert(numbers.join(" and "));

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    A standard way is to create an empty array and raise a flag for each value that you're already picked.

    Code javascript:
    function randomNumber(limit) {
        return Math.floor(Math.random() * limit);
    }
    var selectedNumbers = [],
    	limit = 3,
    	number = 0,
    	result = '',
    	i;
    for (i = 0; i < limit; i += 1) {
    	number = randomNumber(limit);
    	while (selectedNumbers[number] === true) {
    	    number = randomNumber(limit);
    	}
    	selectedNumbers[number] = true;
    	if (result > '') {
    		result += ' and ';
    	}
    	result += number;
    }
    alert(result);
    Last edited by paul_wilkins; Sep 18, 2008 at 15:47.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Evangelist jor133d's Avatar
    Join Date
    Jul 2003
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! Thanks! Both these solutions get me one step closer but I have a follow-up question.

    I need to use the numbers later on in my html document. How to I separate the numbers into different variables?

    Thanks again!

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The array method already stores the values as numbers[0], numbers[1], etc. If necessary you can assign them to other variables:
    Code:
    var myVarName = numbers[0];
    var myVarForLater = numbers[1];

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    If you store the result string somewhere you can use
    resultArray = result.split(' and ') to end up with an indexable array of the numbers.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm starting to doubt my method now! I'm not convinced it will give a uniform distribution, it depends on the sorting algorithm. The random number in the sort function may have to be tweaked.

  8. #8
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, here's a new method.
    Code:
    var numbers = [1, 2, 3], sorted = [];
    while (numbers.length){
      sorted.push(numbers.splice(Math.random() * numbers.length, 1));
    }
    alert(sorted.join(" and "));
    It seems to work without Math.floor but you may want to add it in. This time it does produce a uniform distribution.

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The person who posted the above code later on said that it doesn't do a good job, they're not very random at all, and came up with a much better way to randomise them.
    http://www.sitepoint.com/forums/show...40&postcount=8

    Working on from there, here is how I suggest that you achieve what you're after.

    Code javascript:
    var videos = [
    	{'index': 1, 'desc': 'This is the text linked to number 1'},
    	{'index': 2, 'desc': 'This is the text linked to number 2'},
    	{'index': 3, 'desc': 'This is the text linked to number 3'},
    	{'index': 4, 'desc': 'This is the text linked to number 4'},
    	{'index': 5, 'desc': 'This is the text linked to number 5'},
    	{'index': 6, 'desc': 'This is the text linked to number 6'},
    	{'index': 7, 'desc': 'This is the text linked to number 7'},
    	{'index': 8, 'desc': 'This is the text linked to number 8'},
    	{'index': 9, 'desc': 'This is the text linked to number 9'},
    	{'index': 10, 'desc': 'This is the text linked to number 10'},
    	{'index': 11, 'desc': 'This is the text linked to number 11'}
    ];
    randomised = [];
    while (videos.length) {
        randomised.push(videos.splice(Math.random() * videos.length, 1));
    }
    var item1 = randomised[0].index;
    var item2 = randomised[1].index;
    var item3 = randomised[2].index;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •