Page 1 of 1
Using Modulo to get a chance of higher than 50%
Posted: Mon Jul 16, 2012 5:08 pm
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.
Re: Using Modulo to get a chance of higher than 50%
Posted: Mon Jul 16, 2012 6:08 pm
by dandymcgee
Code: Select all
//Generate random number between 1 and 100
if(randomNumber <= 57)
{
//This has a 57% chance of happening
}
Re: Using Modulo to get a chance of higher than 50%
Posted: Mon Jul 16, 2012 6:52 pm
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
}
}
Re: Using Modulo to get a chance of higher than 50%
Posted: Mon Jul 16, 2012 7:22 pm
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
Re: Using Modulo to get a chance of higher than 50%
Posted: Thu Jul 19, 2012 5:16 pm
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.
Re: Using Modulo to get a chance of higher than 50%
Posted: Thu Jul 19, 2012 9:47 pm
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...
}
Re: Using Modulo to get a chance of higher than 50%
Posted: Tue Jul 24, 2012 2:19 pm
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.
Re: Using Modulo to get a chance of higher than 50%
Posted: Wed Jul 25, 2012 4:31 pm
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.