int i = rand%10+1; if(i/3 == int) do this
2 replies to this topic
Sponsor:
#2 Moderator* - Reputation: 5391
Posted 13 August 2012 - 11:22 PM
You're doing it right there in your code (just above the if statement). See that % operator? That's the modulus operator. Simplified explanation, a % b tells you the remainder when you divide a by b, and if the remainder is zero, a is in fact a multiple of b, and the result of a / b is an integer. So you would just do:
if (i % 3 == 0)
// i is divisible by 3, and thus i / 3 is an integer
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
#3 Moderators - Reputation: 7562
Posted 13 August 2012 - 11:55 PM
Depends on the language you're using.
In C, C++, C#, and Java (my best guesses from the snippet you posted) values do not change types at runtime. So if you write this code:
Then foo is an integer, and always will be. If you try to assign anything else to it, you either get a compiler error, or you get the integer part of whatever number you tried to assign, if you use a cast.
If you're talking about a different language, which actually does support "dynamic types" (i.e. types of variables/values can change at runtime) then you'll need to specify the language, as they all differ in how you test for a specific type.
In C, C++, C#, and Java (my best guesses from the snippet you posted) values do not change types at runtime. So if you write this code:
int foo = [some expression];
Then foo is an integer, and always will be. If you try to assign anything else to it, you either get a compiler error, or you get the integer part of whatever number you tried to assign, if you use a cast.
If you're talking about a different language, which actually does support "dynamic types" (i.e. types of variables/values can change at runtime) then you'll need to specify the language, as they all differ in how you test for a specific type.
Maker of Machinery
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]






