Forcing a randomly generated number to always be odd

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
Nash
 
 
Posts: 17501
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Forcing a randomly generated number to always be odd

Post by Nash »

I want to generate a random number and have its result always guaranteed to be an odd number. How do I do this?
User avatar
InsanityBringer
Posts: 3392
Joined: Thu Jul 05, 2007 4:53 pm
Location: opening the forbidden box

Re: Forcing a randomly generated number to always be odd

Post by InsanityBringer »

A cheap and silly way would probably work like this:

Code: Select all

if (randVal % 2 == 0) //check if it's even
{
    randVal++; //make it not even if that is the case
}
User avatar
Enjay
 
 
Posts: 27164
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Re: Forcing a randomly generated number to always be odd

Post by Enjay »

I was going to suggest checking if it was divisible by 2 and if is re-roll but IB's suggestion of simply increasing any even number by 1 is much neater because it avoids the re-roll (which, although unlikely, could lead to a long loop of randomly generated even numbers being rejected).
User avatar
Nash
 
 
Posts: 17501
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Forcing a randomly generated number to always be odd

Post by Nash »

Okay thanks... and what if I want the result to be guaranteed even? Sorry but I'm dumb.
User avatar
printz
Posts: 2649
Joined: Thu Oct 26, 2006 12:08 pm
Location: Bucharest, Romania
Contact:

Re: Forcing a randomly generated number to always be odd

Post by printz »

Nash wrote:I want to generate a random number and have its result always guaranteed to be an odd number. How do I do this?
2*n - 1, where n is an integer.
Gez
 
 
Posts: 17946
Joined: Fri Jul 06, 2007 3:22 pm

Re: Forcing a randomly generated number to always be odd

Post by Gez »

Yeah, that's the basic idea. Take a (integer) number, multiply it by two, you'll be sure to have an even number. Take an even number, add or subtract an odd number (such as 1), and you'll be sure to have an odd number.

For example, if you want an odd number between 1 and 99 included, you can use rand(1, 50)*2-1 or rand(0, 49)*2+1.
User avatar
DavidPH
Posts: 382
Joined: Fri Aug 28, 2009 1:46 pm

Re: Forcing a randomly generated number to always be odd

Post by DavidPH »

Or (randval & ~1) for even and ((randval & ~1) + 1) for odd. To get a random odd number between N and M, ((rand(N, M-1) & ~1) + 1). Has the advantage of the number being a little more obvious. (And possibly marginal execution speed increase.)
User avatar
randi
Site Admin
Posts: 7749
Joined: Wed Jul 09, 2003 10:30 pm
Contact:

Re: Forcing a randomly generated number to always be odd

Post by randi »

DavidPH wrote:To get a random odd number between N and M, ((rand(N, M-1) & ~1) + 1)
Just use rand(N, M-1) | 1 Odd numbers always have the first bit set, and even numbers always have it cleared.
Locked

Return to “Editing (Archive)”