Using Modulo to get a chance of higher than 50%

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
SomeAwesomeUsername
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 21
Joined: Mon Jul 16, 2012 4:32 pm

Using Modulo to get a chance of higher than 50%

Post by SomeAwesomeUsername »

Using a random seed, how can I get a number higher 50% chance?

//if(seed % 2 == 0) // This is a 50% chance
//if(seed % 3 == 0) // This is a 33% chance
//if((seed % 2 == 0) && (seed % 3 == 0)) //Same as 1/6th, 16% Chance

considering 1/2 is 50%, 1/1.5 would be 66.6%, but when working with whole numbers it ends up being a 33.3% chance because 1.5 only goes into multiples of 3.

If I cant use modulo, then does anyone have another suggestion of using any other method beside modulo to get higher than a 50% chance? I'm using a seed so that I can save the seed and reload it.

I'm using JavaScript if that changes anything.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Using Modulo to get a chance of higher than 50%

Post by dandymcgee »

Code: Select all

//Generate random number between 1 and 100

if(randomNumber <= 57)
{
	//This has a 57% chance of happening
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
SomeAwesomeUsername
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 21
Joined: Mon Jul 16, 2012 4:32 pm

Re: Using Modulo to get a chance of higher than 50%

Post by SomeAwesomeUsername »

I originally was doing that, but want to keep to one variable.

I'm using more than 1 random number in my program and eventually want to save everything that's randomized to an XML file.

I may be wrong, but I was thinking that having one variable would be easier to store to the XML file and be more efficient than creating multiple variables.

Code: Select all

int seed;

if(seed % /*some magical number to get above 50%*/ == 0)
{
   //57% chance
   if(seed % /*some magical number to get above 50%*/ == 0)
   {
      //57% chance
   }
}
compared to 2 variables

Code: Select all

int randomNumber;
int randomNumber2;

if(randomNumber <= 57)
{
   //This has a 57% chance of happening
   if(randomNumber2 <= 57)
   {
      //This has a 57% chance of happening
   }
}
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Using Modulo to get a chance of higher than 50%

Post by qpHalcy0n »

Can't be sure exactly what the goal is here but if you try something like:

rand() % 1024, you'll always get a value between 0 and 1023. rand() % 512 and you'll get something between 0 and 511.
So divide by 1024.0f or 512.0f as it were and you'll get some value 0-0.99999. There's your percentage.


rand() % 1024 / 1024.0f
CC Ricers
Chaos Rift Regular
Chaos Rift Regular
Posts: 120
Joined: Sat Jan 24, 2009 1:36 am
Location: Chicago, IL

Re: Using Modulo to get a chance of higher than 50%

Post by CC Ricers »

(edit: I didn't realize you want to use just one seed variable. Any reason why?

Maybe you can consider shifting a few bits for a second comparison. If the integer is 32 bits

(seed % 2 == 0) || ((seed >> 16) % 2 == 0)

Would give you a 75% chance of being true.
SomeAwesomeUsername
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 21
Joined: Mon Jul 16, 2012 4:32 pm

Re: Using Modulo to get a chance of higher than 50%

Post by SomeAwesomeUsername »

(edit: I didn't realize you want to use just one seed variable. Any reason why?

Maybe you can consider shifting a few bits for a second comparison. If the integer is 32 bits

(seed % 2 == 0) || ((seed >> 16) % 2 == 0)

Would give you a 75% chance of being true.
Thank you!

I'm loading game objects into a scene randomly, originally I was checking at every if statement for a new random variable between 1 and 10, but I thought computing with the seed would be more efficient. I also want to be able to reload the game level by saving the seed then using it to recreate the scene.

Code: Select all

if(seed % 2 == 0)
{
     if((seed % 919/*prime number*/ == 0) && (seed % 911 != 0))
     {
          //load object 1 at position 1
     }
     else if((seed % 919 != 0) && (seed % 911 == 0))
     {
         //load object 2 at position 1
     }
}
else if(seed % 3 == 0)
{
   //etc...
}
CC Ricers
Chaos Rift Regular
Chaos Rift Regular
Posts: 120
Joined: Sat Jan 24, 2009 1:36 am
Location: Chicago, IL

Re: Using Modulo to get a chance of higher than 50%

Post by CC Ricers »

Actually, now that I think about it, the number of bits you shift the integer to the right doesn't matter because each bit has an equal chance of being 1 or 0. As with using a divisor other than 2, the idea is almost the same because shifting a bit will give you a new number. Depending on what divisor you use, the remainder may or may not be different from the original number's, so you still keep a degree of uncertainty.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Using Modulo to get a chance of higher than 50%

Post by Ginto8 »

Given some random unsigned integer x, you can do things like this:

Code: Select all

if(x%y == z) // gives a 1/y chance
if(x%y < z)  // gives a z/y chance
if(x%y >= z) // gives a (y-z)/y chance
You can use other comparisons as well, but these are basic and useful.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply