random number

why i get the same output when i compile and run the program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;                                                                         
main()
{
	int z;
	int trynum;
	int i;
	srand(time(0));
	z= rand()/327.67;                                 
	while(trynum!=z && i<6)
	{
		cin >> trynum;
		i = i+1;
}
cout << endl << "value is " << z;
}
ok... lets take it from the top.
its <cstdlib> and <ctime> in c++
using namespace std .. see web or topics here on why its bad practice. you can use it for now, but at least be aware.
its int main. no type on main is not legal though some compilers support it.
7,8,9 are uninitialized. this is your problem, it prevents i from being 0 so the loop does not execute most likely.

10... consider using the tools in <random> instead of rand. I know rand is easy, but you can copy and paste 5 lines from the example on this site and be right back where you were with better tools.

11 perhaps a % is better here? x%100 gives a value from 0 to 99, and so on: its the 'remainder' in integer division.

15 i++; is expected in c++ for adding 1. += is expected for adding other values to itself eg i+=3; what you have works fine.

if you get the same random value every time you run it, that is a problem with your compiler or a setting, not your code. your code is actually FINE apart from the i = 0 issue. which you can (and should) initialize with int i{}; //zero or int i{42}; //value when you declare it.
Last edited on
trynum and i are uninitialized. The compiler should warn you about this.

You cannot predict what the value of those variables are. i is probably greater than 6. So you might not get into the loop.
But i might also be less than 0. Then the loop goes on for a long time.

So I suggest to initialize the variables properly and you will get the expected result.
coder777 wrote:
You cannot predict what the value of those variables are. i is probably greater than 6. So you might not get into the loop.
But i might also be less than 0. Then the loop goes on for a long time.

Or the compiler might assume the loop is never reached (because reading uninitialized variables is technically UB) and do optimisations based on that which could lead to some very unexpected results.
Last edited on
Conforming (somewhat) to modern C++ coding practices:
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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
   srand(static_cast<unsigned>(time(0)));

   int trynum { };
   int i      { };

   // what exactly is the distribution of random numbers expected?  min?  max?
   // int z      { rand() / 327.67 };  // why divide an int with a double?

   // conversion from double to int, possible loss of data
   int z      = rand() / 327.67;

   while ( trynum != z && i < 6 )
   {
      std::cin >> trynum;
      i++;
   }

   std::cout << "\nValue is " << z << '\n';
}

No prompt to let the user know what to enter:
a

Value is 29

This looks like a number guessing game with 6 max guesses. There are other loop styles that might work better. A for loop might be a better choice.

If this is a class assignment posting the requirements here would be helpful for us to better tweak your code.
// int z { rand() / 327.67 }; // why divide an int with a double?

32768 or 32767 is the 16 bit integer value, it looks like some sort of normalization based off that, but since its probably a 32 bit integer from rand, I got nothing on the WHY or WHAT.
if rand gave 16 bit ints (eg, if this code were 50 years old) it would scale it to 0-100 or whatever.
or maybe this is deluded oldfartery and he has a completely different idea :)
Last edited on
Even if the 'divide an int by a double' didn't result in a rather nasty bit of possible lost of data conversion the distribution of the resulting random number is still rather expansive.

As distastefully biased as int z = rand % 100 + 1 clamping is, the way it is done in the OP's code is even more potentially error-prone and non-portable.

That bit of weird clamping looks like some "hey, look at me and what I can do!" nonsense found on the internet.

Let's presume the distribution is supposed to be 0 - 100, C++ code to generate a random number using <random> (plus a prompt so the user knows what is going on):
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
#include <iostream>
#include <random>

int main()
{
   // create a C++ random engine
   std::default_random_engine rng;

   // let's seed it using the non-deterministic random engine
   rng.seed(std::random_device{}());

   int trynum { };
   int i      { };

   // what exactly is the distribution of random numbers expected?  min?  max?
   // int z      { rand() / 327.67 };  // why divide an int with a double?

   // conversion from double to int, possible loss of data
   // int z = rand() / 327.67;

   // let's presume the distribution (min/max) wanted is 0 - 100
   std::uniform_int_distribution<int> dist(0, 100);

   // let's get us a random number the C++ way
   int z { dist(rng) };

   // let's show what the number chosen is for testing purposes
   std::cout << "(" << z << ")\n\n";

   std::cout << "You have six tries to guess 'the number' ( 0 - 100 ):\n";

   while ( trynum != z && i < 6 )
   {
      std::cin >> trynum;
      i++;
   }

   std::cout << "\nValue is " << z << '\n';
}

('Guessing' the chosen number):
(39)

You have six tries to guess 'the number' ( 0 - 100 ):
50
25
39

Value is 39

(Not 'guessing' the number):
(21)

You have six tries to guess 'the number' ( 0 - 100 ):
50
25
75
12
85
44

Value is 21

Remove/comment out line 28 and now the user truly does have to guess without a hint other than the range of the distribution.
Topic archived. No new replies allowed.