Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8463555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:29:12+00:00 2026-06-10T14:29:12+00:00

I am trying to make a program generating random numbers until it finds a

  • 0

I am trying to make a program generating random numbers until it finds a predefined set of numbers (eg. if I had a set of my 5 favourite numbers, how many times would I need to play for the computer to randomly find the same numbers). I have written a simple program but don’t understand the outcome which seems to be slightly unrelated to what I expected, for example the outcome does not necessarily contain all of the predefined numbers sometimes it does (and even that doesn’t stop the loop from running). I think that the problem lies in the logical operator ‘&&’ but am not sure. Here is the code:

const int one = 1;
const int two = 2;
const int three = 3;

using namespace std;

int main()
{

    int first, second, third;
    int i = 0;

    time_t seconds;
    time(&seconds);

    srand ((unsigned int) seconds);

    do

    {


    first = rand() % 10 + 1;
    second = rand() % 10 + 1;
    third = rand() % 10 + 1;


    i++;

    cout << first<<","<<second<<","<<third<< endl;
    cout <<i<<endl;
    } while (first != one && second != two && third != three); 

    return 0;
 }

and here is out of the possible outcomes:

3,10,4
1 // itineration variable
7,10,4
2
4,4,6
3
3,5,6
4
7,1,8
5
5,4,2
6
2,5,7
7
2,4,7
8
8,4,9
9
7,4,4
10
8,6,5
11
3,2,7
12

I have also noticed that If I use the || operator instead of && the loop will execute until it finds the exact numbers respecting the order in which the variables were set (here: 1,2,3). This is better however what shall I do make the loop stop even if the order is not the same, only the numbers? Thanks for your answers and help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:29:14+00:00Added an answer on June 10, 2026 at 2:29 pm

    The issue is here in your condition:

    } while (first != one && second != two && third != three);  
    

    You continue while none of them is equal. But once at least one of them is equal, you stop/leave the loop.

    To fix this, use logical or (||) rather than a logical and (&&) to link the tests:

    } while (first != one || second != two || third != three);  
    

    Now it will continue as long as any of them doesn’t match.

    Edit – for a more advanced comparison:

    I’ll be using a simple macro to make it easier to read:

    #define isoneof(x,a,b,c) ((x) == (a) || (x) == (b) || (x) == (c))
    

    Note that there are different approaches you could use.

    } while(!isoneof(first, one, two, three) || !isoneof(second, one, two, three) || !isoneof(third, one, two, three))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to make a program that retrieves an endless amount of numbers that
I'm trying to make a program that can be hosted by many peoples, like
I'm trying to make my program save its state in the location set by
I'm trying to make a program that gets two input numbers, multiplies them (storing
I'm trying to make a program, which would run cl.exe (C/C++ compiler) and compile
I am trying to make a program that if you would press a button,
So im trying to make a program that asks for 3 numbers and then
I'm trying to make a program that, using pointers, detects lines in an image
I'm trying to make a program that lets you input any one-step equation, and
I'm trying to make a program which will create a sum of directories the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.