Random number

Hello, I am supposed to write a code that has the computer guess my number. The specification though is that the number is 4 digits long and only uses the numbers 1-4. Example being 1111, 1234, 4444. I understand how to make a random number generator. I just don't understand how to make the code so specific. So could someone point me in the right direction.
Thank you.

There is a neat work around. Generate 4 random digits 1 - 4. You could store them in an array and then check guesses against that. You can give clues such as higher or lower by checking if each digit, starting from the greatest, is higher or lower than the guess.
closed account (1v5E3TCk)
1) declare two integers: int foo, bar = 0;
1) declare foo randomly.
2) multiply foo by 1000 and add it to bar.
3) declare foo randomly again.
4) multiply foo by 100 and add it to bar.
5) declare foo randomly again.
6) multiply foo by 10 and add it to bar.
7) declare foo randomly again.
8) add foo to bar.
9) try to guess bar.

Good Luck!
I still can not seem to get my code to only use 1-4
My code is listed below. What have I done wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;

int main()
{

    int tries = 0,array,uNum,rNum,max=4444,min=1111;


    cout << " Enter number (1111-4444): " << endl;
    cin >> uNum;

    srand(time(0));
	int j=1, num=0;
	for (int i=0; i<=3; i++)
{
      num = num + (rand() % 4 + 1) * j;
      j *= 10;
} 

    do
	{
        rNum = min + rand() % (max - min + 1);

        cout << " Computer Guessed " << rNum << endl;

        if(rNum > uNum)
            {
                max = rNum - 1;
            }
        else if(rNum < uNum)
            {
                min = rNum + 1;
            }


    }while(rNum != uNum);

    cout << num << endl;

    return 0;
}
Anything at all?
Min should be 1 and max should be 4.

Then did you even try what they mentioned?

Basically it is like this:
Get a random number append
Get a random number append
Get a random number append
Get a random number append

Then you are left with some random number , some random number , some random number , some random number.


So if you get 1 , 2 , 3 , 4 then it should be 1234. How do we get this?

well 1 should be multplied by 10^3 then 2 should be multiplied by 10^2 , 3 by 10^1 and 4 by 10^0.


enough pseudo code...

for the sake of simplictity I will do it reverse order so if the random numbers gather are 1 2 3 4 I will do
1 * 10^0 , 2 * 10^1 , 3 * 10^2 , 3 * 10^3

so it will be 4321 when added.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream> //cout , cin , endl
#include <ctime> //time
#include <cstdlib> //srand , rand

int main()
{
    srand( time(NULL) );
    int number; //input for player
    int digits = 0 , power = 1;
    int min , max;
    int guess , random;
    int count = 0;
    int temp;

    std::cout << "Please enter a number to be guessed: ";
    std::cin >> number;
    
    temp = number;
    while( temp > 0 )
    {
        ++digits;
         temp /= 10;
    }

    std::cout << "Please enter the minimum digit: ";
    std::cin >> min;
    std::cout << "Please enter the maximum digit: ";
    std::cin >> max;


    while( guess != number )
    {
       ++count;
        guess = 0;
        power = 1;
        for( int i = 0; i < digits; ++i )
        {
            random = rand() % ( max - min + 1 ) + min;
            guess += random * power;
            power *= 10;
        }
        std::cout << "The computer guessed: " << guess << std::endl;
        std::cout << "The answer is: " << number << std::endl; 
    }
    std::cout << "The computer took " << count << " tries to guess the number." << std::endl;
}

closed account (1v5E3TCk)

So if you get 1 , 2 , 3 , 4 then it should be 1234. How do we get this?

well 1 should be multplied by 10^3 then 2 should be multiplied by 10^2 , 3 by 10^1 and 4 by 10^0.


enough pseudo code...

for the sake of simplictity I will do it reverse order so if the random numbers gather are 1 2 3 4 I will do
1 * 10^0 , 2 * 10^1 , 3 * 10^2 , 3 * 10^3

so it will be 4321 when added.


that is what I meant but I think he didnt did what I said
Yeah I was just mentioning it again since he didn't seem to read it.
did you even try what they[mats and senhor] mentioned?
closed account (1v5E3TCk)
uh sorry :D
Topic archived. No new replies allowed.