### How do i generate a random number but exclude some of them?

alright,so i need to generate a random number.
its a number between 1-54
this is a bad explanation of the program feel free to skip
---------------------------------
the program draws cards so thats why its 54
now the thing is,i need the program to draw a specific set of cards,and end when it draws them.
the way it checks if the cards are the ones that i need is because the sum of their value is below 33.
or atleast thats what i thought,turns out you can make the value of 33 with other combos aswell.
--------------------------------------------
so back to the question,i need a random number generated from 1-8,and then from 19-65.
i thought about checking with an if statement,but i dont think that would work since it would check only once or twice,and then go on..
so is there any easier way.
 ``123456789101112131415`` `````` srand( time(NULL)); int CardsRolled[6]; int sum=34; int numb=0; while(sum>33){ sum=0; cout<<"\n"; cout<

this is the part of the code that generates the random number
 ``1234567891011121314151617`` ``````#include #include using namespace std; int random(int from, int to) { return from + rand() % (to - from + 1); } int main() { // I need a random number generated from 1-8, and then from 19-65. cout << random(1, 8) << endl; cout << random(19, 65) << endl; }``````
thanks for the code bro,but wouldn't this generate two numbers,one from 1-8
and one from 19-65.
i think i worded the question wrong so its my fault.
i need 1 number to be generated that follows those rules.
 ``12345678910111213141516171819202122232425262728`` ``````#include #include #include using namespace std; int random(int from, int to) { return from + rand() % (to - from + 1); } int main() { // I need a random number generated from 1-8, and then from 19-65. srand(time(nullptr)); int myRand; if (random(0,1)) { myRand = random(1, 8); } else { myRand = random(19, 65); } cout << myRand << endl; }``````

:)
EDIT: seeded the rng
Last edited on
alright thanks to both of you i think i know what to do now
The method I showed probably isn't what you want. Since one of those ranges is larger than the other, the fact that either range is decided upon by coin-toss practically means that each number in the smaller range will appear more frequently than any number in the larger range.
Argh. Two suggestions:

1. Split the range. (1-8 == 9 cards) + (19-65 == 47 cards) = 56 cards.

 ``12`` ``````int c = get_random_number_in( 1, 56 ); if (c > 8) c += 10; // 9-->19, etc. ``````

2. Throw away invalid numbers

 ``1234`` ``````int c; do { c = get_random_number_in( 1, 65 ); } while ((c > 8) && (c < 19));``````

Hope this helps.
What CARD game are we emulating here OP? Because I have a feeling that you're going about this all wrong. Random number generation is not your solution to this.

You're talking about a standard deck of cards, 54 cards so that's 40 number cards, 12 faces and 2 jokers correct? Since you can only draw a card once, you need to prevent duplicate entries. Now you could keep building on the random number generation like you asked for until it becomes a complete mess of code. Or you could populate a vector with the values of each card in the deck, then apply std::random_shuffle() to that vector and pop the results off the top of the array just like you do with a real deck of cards in meat space. I can post some code if you want, but the code I had in mind to copy and paste into here is basically me screwing around with Lambda's so I'd have to clean it up and make it readable first.
@booradley yes,the method you gave me worked,but what you said happened,and thats why the program didn't work correctly
thanks for the reply computer geek.
the card game i am emulating is called makao where i live.'
the rules are unimportant,what is important for this is that,the A cards and the 8 cards are cards used for skipping a player.so if i throw and A spade for example,the next player loses his turn.
this program is just drawing cards untill a full hand of cards that skip is drawn(one hand consists of 6 cards).
and each time a hand is drawn the deck is reset.
also,i have VERY limited experience in c++,and i sort of challenged myself to write this program no matter how bad,unoptimized or stupid it is.
i have no idea how to use half of the things you said.
i will post the code to my program here,its long but 90% of it is just repetition i could have probably skipped if i had more experience.
the program needs to end only when a hand consists of only 8ts and Aces
and not any other number.i think i messed up in the checking of the hand part so it doesn't work now.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285`` ``````#include #include #include #include using namespace std; int main(){ srand( time(NULL)); int CardsRolled[6]; int sum=34; int numb=0; while(sum>33||CardsRolled[0,1,2,3,4,5]==9||10||11||12||13||14||15||16||17||18){ sum=0; cout<<"\n"; cout<

using this code it just runs forever
Last edited on
Topic archived. No new replies allowed.