🔒 Locked
Random number
Advertisement
Original Post
i need a code to chose a even random number between 10 and 36 using C++ , and i am very new at this [Edited by - Program_mike on June 19, 2007 8:21:47 PM]
In C++:
This is assuming that 10 and 36 are included.
int num = 1;while ((num%2) != 0) num = rand()%27 + 10;This is assuming that 10 and 36 are included.
Quote:
Original post by Chaotic_Attractor
In C++:
*** Source Snippet Removed ***
This is assuming that 10 and 36 are included.
The problem with that method is the possibility (though miniscule) of becoming an infinite loop if odd numbers kept coming up. As a rule, I try to avoid loops that last a random amount of time before coming up with the result I want.
A better method would be to get a number between 5 and 18 (just once every time) and then double it.
Quote:
The problem with that method is the possibility (though miniscule) of becoming an infinite loop if odd numbers kept coming up.
I suppose so, but the chance of that actually affecting the performance of the program is so low that it is practically zero. And technically, you could not have an infinite loop here since at infinity 53.8% of the numbers chosen would be even.
True, it couldn't become an infinite loop, but it's still an indefinite loop. Why not avoid the indefinite method if there's something as easy as rnd(5-18) * 2 available?
Sorry to be nitpicky, but that's even worse. There's exactly half the probability of 10 at the output than any one of the values 12-36. Using 9-36 as the range would work though.
Quote:
Original post by owl
something among the pseudo-line of:int Random_Even(){ int number = Random_Number(10,36); if (is_Odd(number)) number++; return number;}
Sorry to be nitpicky, but that's even worse. There's exactly half the probability of 10 at the output than any one of the values 12-36. Using 9-36 as the range would work though.
Quote:
Original post by owl
something among the pseudo-line of:int Random_Even(){ int number = Random_Number(10,36); if (is_Odd(number)) number++; return number;}
it says "
implicit declaration of function `int Random_Number(...)'
Quote:
Original post by Program_mike Quote:
Original post by owl
something among the pseudo-line of:int Random_Even(){ int number = Random_Number(10,36); if (is_Odd(number)) number++; return number;}
it says "
implicit declaration of function `int Random_Number(...)'
Hence the "pseudo-line", a reference to "psuedo-code", which means the code should look something like this. You would need to write "Random_Number" and "is_Odd" yourself for the above to compile.
Pzc gave you code which could be used as the body of a Random_Number() function, and Chaotic_Attractor has a snippet which contains (among other things) an implementation of the body of is_Odd.
Try yourself and see how far you get.
Quote:
Original post by Bregma Quote:Ah, I think you mean this.
Original post by yadango
13 ;)int val = (rand() % 13 + 5) * 2;
does that work?
% 13 returns 0 to 12...
+ 5 so 5 to 17...
times 2 so 10 to 34...
so should be % 14 :-).
but no he he he, that's not what i meant lol. i was just having some fun. his original post (before he edited it) was "i need a random number between 10 and 36." :-)
int Val=((rand() % 26) + 10) & 126;
Quote:
Original post by rip-off Quote:
Original post by Program_mike Quote:
Original post by owl
something among the pseudo-line of:int Random_Even(){ int number = Random_Number(10,36); if (is_Odd(number)) number++; return number;}
it says "
implicit declaration of function `int Random_Number(...)'
Hence the "pseudo-line", a reference to "psuedo-code", which means the code should look something like this. You would need to write "Random_Number" and "is_Odd" yourself for the above to compile.
Pzc gave you code which could be used as the body of a Random_Number() function, and Chaotic_Attractor has a snippet which contains (among other things) an implementation of the body of is_Odd.
Try yourself and see how far you get.
could you help me with those?
Quote:
Original post by instinKt Quote:
Original post by Zahlman Quote:
Original post by Program_mike
[choose an] even random number between 10 and 36 using C++
What for?
I smell homework.
no just making a simple mind/math game
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement