I am planning to create a simple bulls and cows( also know as mastermind board game) in C.The computer is supposed to make 4 digit code in which the allowed digits are 1,2,3,4,5,and 6 ! 0,7,8,9 are to omitted !! How do I make the computer create such a 4 digit number? I know about rand() function !! But how do I make it omit those 4 digits?
jeevsmyd
-2
Junior Poster
Recommended Answers
Jump to Postrand() can give you a specific range of random numbers. Just set it up for 1 to 5, four times.
1
2
3
5Inside a loop, you can put the "number" together, from those digits if you want:
5 * 10^0 +
3 * 10^1 +
2 …
Jump to PostUsing modulo you can generate a number up to pretty much anything:
int number = rand();
generates a number between 0 and the max your system can handle (likely, 2^32 / 2).
int number = rand() % 6;
generates a number between 0 and …
All 8 Replies
Adak
419
Nearly a Posting Virtuoso
Auraomega
2
Junior Poster in Training
jeevsmyd
-2
Junior Poster
Adak
419
Nearly a Posting Virtuoso
jeevsmyd
-2
Junior Poster
Auraomega
2
Junior Poster in Training
jeevsmyd
-2
Junior Poster
Auraomega
2
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.