Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16

Thread: Math Problem

  1. #1
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    I am tired, so I am not sure if it is my math or coding that is the problem here...
    Code:
    	r1 = (Math.random()*511)+1;
    	if (256 < r1 < 512)
    	{
    		int1 = 1;
    	}
    	else if (128 < r1 < 256)
    	{
    		int1 = 2;
    	}
    	else if (64 < r1 < 128)
    	{
    		int1 = 3;
    	}
    	else if (32 < r1 < 64)
    	{
    		int1 = 4;
    	}
    	else if (16 < r1 < 32)
    	{
    		int1 = 5;
    	}
    	else if (8 < r1 < 16)
    	{
    		int1 = 6;
    	}
    	else if (4 < r1 < 8)
    	{
    		int1 = 7;
    	}
    	else if (2 < r1 < 4)
    	{
    		int1 = 8;
    	}
    	else if (1 < r1 < 2)
    	{
    		int1 = 9;
    	}
    This script generates a random number (between 1 and 512), then there are a number of if statements which have differing chances of being true (1/2, 1/4, 1/8, 1/16, 1/32, 1/64 etc.).

    But...

    It doesn't work, it just returns 1 each time.
    JR

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Try this:
    Code:
    	r1 = (Math.random()*511)+1;
    	if (r1 >= 256) {
    		int1 = 1;
    	} else if (r1 >= 128 ) {
    		int1 = 2;
    	} else if (r1 >= 64) {
    		int1 = 3;
    	} else if (r1 >= 32) {
    		int1 = 4;
    	} else if (r1 >= 16) {
    		int1 = 5;
    	} else if (r1 >= 8) {
    		int1 = 6;
    	} else if (r1 >= 4) {
    		int1 = 7;
    	} else if (r1 >= 2) {
    		int1 = 8;
    	} else {
    		int1 = 9;
    	}

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    They're all powers of 2...surely sqrt or something similar would be useful?
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  5. #4
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Thanks
    JR

  6. #5
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Here's a mathematical increasing probability chooser (you can set the number of choices and increasing probability of each choice:
    Code:
    <script>
    var number = 9;    //number of possibilities you want
    var scale = 2;     //scale of increasing possibility per increment.
    var seed = Math.pow(scale, number);  //random number seed
    var pick = Math.random() * seed + 1;
    for (var i = (number - 1); i >= 0; i --) {
        if (pick >= Math.pow(scale, i)) {
    	int1 = number - i;
    	break;
        }
    }
    </script>
    EDIT: made some minor mods to it. This will return the same values as the option above

  7. #6
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Thanks

    Its for my game. I screwed it all up because the biggest probability was 1/512, cube it (what you would need to win, I am not telling you what my game is yet though ) and it becomes 1/134 217 728 !!!

    So I need a re-think
    JR

  8. #7
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    The last post I made allows you to increase probabilities by a cubic degree... just change the "scale" variable to 3.

  9. #8
    Senior Member james's Avatar
    Join Date
    May 2003
    Location
    Melbourne, AUSTRALIA
    Posts
    364
    Member #
    1352
    Sounds like a log problem.

    Code:
    var Max=512;
    var Base=2;
    var RandNum = Math.round(Math.random()*512);
    var Power = Math.log(RandNum)/Math.log(Base);
    intl = 9-Math.round(Power);
    You might want to replace Math.round with Math.floor
    James H
    Home Page · Mars Page · www.fihsf1.net (formerly www·fihs·net)

  10. #9
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Very nice solution. Logarithms are what I was missing. Damn you :-P

  11. #10
    Senior Member james's Avatar
    Join Date
    May 2003
    Location
    Melbourne, AUSTRALIA
    Posts
    364
    Member #
    1352
    I do Physics, so it was kind of duh!
    James H
    Home Page · Mars Page · www.fihsf1.net (formerly www·fihs·net)


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 08:06 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com