Results 1 to 10 of 10

Thread: math.random with exclusion

  1. #1
    Join Date
    Mar 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default math.random with exclusion

    Okay I've scoured Google and this forum but haven't found what I need or anything similiar (even though this shouldn't be too tough).

    What I have is a script that displays 3 of 5 div areas on a web page.

    The problem is that whenever math.random returns the same number it obviously won't keep to my 'showing 3 items at all times' model and just displays 2 or 1 item(s).

    The script:

    function randomize(){
    var rand = 0;
    var num = 0;

    rand = Math.round(Math.random() * 5);
    num = rand;

    document.getElementById('c'+rand).style.display='';
    }

    I also have a script that loops 3 times through the random method:

    function startRand(){

    var many = 3;


    for(var i=0;i<many;i++){
    randomize();
    }

    }

    Thanks in advance!

  2. #2
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I suggest you use if - else and if else in your script... (i am thinking you know how to do that)

  3. #3
    Join Date
    Mar 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    of course lol

    I am a php/asp programmer (not so hot with the JS ) and what I can't figure out with the javascript is how to set the variable to be remembered to exclude the number...

    so in short, (pseudo)

    num = math.random;
    rand = num;

    while(num != rand){
    document.getElementById('c'+rand).style.display='';
    }

    or some variation of this, but I just cannot get it working, it won't 'remember' the var num (hence my question here ).

    Cheers

  4. #4
    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

    This litte bit:

    Code:
    var many = 3;
    
    
    for(var i=0;i<many;i++){
    randomize();
    }
    will run randomize() 4 times (0 through 3, the value of many). It will run once each loop of the for statement producing a different random number each time. If you want to use only one random number for all the loops, set it outside of the for statement.
    - John
    ________________________

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

  5. #5
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Possible solution

    I know its long, but its comprehensive.

    Code:
    //Robs Fantastic Random Number Generator
    
    RNA = new Array();//Random Number Array, Stores Random Numbers for accessing later
    
    var lowest_number = 1;		//lowest number wanted
    var highest_number = 10;	//highest number wanted
    var unique_numbers = true;	//if each number is unique, true=all numbers different, fasle=numbers may be the same
    var amount_of_numbers = 10;	//amount of numbers you want
    
    
    //fill next free array slot with a random number
    function addRandomNumber()
    	{
    	foundUniqueNumber = false;
    	range = (highest_number-lowest_number)+1;
    	
    	if(!unique_numbers)
    		{
    		RNA[RNA.length] = Math.floor(Math.random()*range) + lowest_number
    		}
    	else
    		{
    		while(foundUniqueNumber == false)
    			{
    			var y = Math.floor(Math.random()*range) + lowest_number;
    			foundUniqueNumber = randomNumberUnique(y)
    			if(foundUniqueNumber)
    				{
    				RNA[RNA.length] = y;
    				}
    			}
    		}
    	}
    
    //Checks if random number already exists in array.
    function randomNumberUnique(r_num)
    	{
    	toReturn = true;
    	for(var x=0; x<RNA.length; x++)
    		{
    		if(RNA[x] == r_num)
    			{
    			toReturn = false;
    			break;
    			}
    		}
    	return toReturn;
    	}
    
    //check if amount of unique numbers required is less than or equal to amount possible
    if(amount_of_numbers <= (highest_number-lowest_number)+1)
    	{
    	for(var z=0; z<amount_of_numbers; z++)
    		{
    		addRandomNumber()
    		}
    	}
    else
    	{
    	alert("You have chosen to have unique numbers, but don't have a large enough range. Try reducing the amount of numbers required or increasing the range.")
    	}
    
    alert(RNA)
    All you need to do is change the top few variables and then the RNA array will be populated with your numbers. You can access them like a normal array.
    Last edited by Bob90; 03-09-2007 at 10:41 PM. Reason: forgot somethiong

  6. #6
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Error in post above

    The code doesn't work if unique numbers are not used because of the if statement at the bottom. I'll fix it tommorrow and repost.

  7. #7
    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

    If all you want are 3 unique random numbers from 0 through 5:

    Code:
    <script type="text/javascript">
    var rand_3=[];
    
    Array.prototype.removeDuplicates = function () {
    	for(var i = 0; i < this.length; i++){
    	for (var i_tem = 0; i_tem < this.length; i_tem++)
    		if(this[i] == this[i_tem]&&i!==i_tem){
    			this.splice(i_tem,1);
    		}
    	}
    }
    
    while(rand_3.length<3){
    rand_3[rand_3.length]=Math.round(Math.random() * 5);
    rand_3.removeDuplicates();
    }
    alert(rand_3.join(','));
    </script>
    - John
    ________________________

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

  8. #8
    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

    Here's an even simpler method (thanks to mwinter's random sort function):

    Code:
    <script type="text/javascript">
    var nums=[0,1,2,3,4,5].sort(function() {return 0.5 - Math.random();})
    alert(nums[0]+','+nums[1]+','+nums[2])
    </script>
    - John
    ________________________

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

  9. #9
    Join Date
    Mar 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    @jscheuer1

    The second script didn't seem to pull unique numbers for some reason but your first function worked like a charm

    Thanks a lot!

    Finished code (in case anyone ever needs it)

    Code:
    function randomize(){
    
    var rand_3=[];
    var many = 3;
    
    Array.prototype.removeDuplicates = function () {
    	for(var i = 0; i < this.length; i++){
    	for (var i_tem = 0; i_tem < this.length; i_tem++)
    		if(this[i] == this[i_tem]&&i!==i_tem){
    			this.splice(i_tem,1);
    		}
    	}
    }
    
    while(rand_3.length<many){
    rand_3[rand_3.length]=Math.round(Math.random() * 5);
    rand_3.removeDuplicates();
    }
    
    for(var x = 0; x < rand_3.length; x++){
    document.getElementById('c'+rand_3[x]).style.display='';
    }
    
    
    }

  10. #10
    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

    The simpler (shorter actually) script will output numbers randomly. I think you just misunderstand how to use them to display your content.

    Where you have:

    Code:
    for(var x = 0; x < rand_3.length; x++){
    document.getElementById('c'+rand_3[x]).style.display='';
    }
    Using the code in my second example would do it like so:

    Code:
    for(var x = 0; x < 3; x++){
    document.getElementById('c'+nums[x]).style.display='';
    }
    - John
    ________________________

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

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
  •