Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Could someone please explain to me why this is not working, its compiling but not giving me the results.. read the comments besides the couts to see what its doing. I have literally tried everything and not having any luck whatsoever.

C++
    int i;
    srand (time(NULL));
    i = (rand() % 99999999 + 1);
cout << i << endl; //This cout displays a 5 digit number that counts upwards every time
i = 0;
    while (i < 99999999)
  {
          i = (rand() % 99999999 + 1);
  }
cout << i << endl; //This one stalls my program
Posted
Updated 16-Jan-22 6:47am
v2
Comments
[no name] 11-May-13 10:53am    
"is not working" is not an informative description of any kind of a technical problem.
Gavon Black 11-May-13 10:56am    
I actually posted the problems I was having on my comments in the code.. but I restated that in the question
[no name] 11-May-13 11:00am    
No actually you did not. The code is doing exactly what you coded it to do. What you are doing is waiting until "i" is less than 99999999 before it breaks out of the while loop and it could take awhile. It is not "stalling" on the line you indicated.
enhzflep 11-May-13 11:12am    
Ouch! Only 1 value for i in 100 million will allow the loop to exit. Not likely to happen any time soon.. I'd be more inclined to generate a 0-9 result, 8 times. I.e generate a number in the range [0..9], multiply it by 10 to the power of it's digit number then repeat.
Perhaps something a little like this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main()
{
int result, digitIndex, curDigit;
int resultNum;
int numResultsWanted = 10;

// init rand seed
srand(time(NULL));

for (resultNum=0; resultNum<numResultsWanted; resultNum++)
{
result = 0;
for (digitIndex=0; digitIndex<8; digitIndex++)
{
curDigit = rand() % 10;
curDigit *= pow(10, digitIndex);
result += curDigit;
}
printf("8 digit random number: %d\n", result);
}
}
Gavon Black 11-May-13 11:04am    
ok.. so I might see that.. but if I just cout i before the while loop and get rid of the while loop i keeps giving me numbers in the 208 range such as 20830 and 20874, 20915 and just continues to go up.. is this how rand is suppose to work

It may depend on the system you are using: if your integer value is 16 bits, then it can never generate an 8 digit number because the largest value it can hold is 2^15 or 32K.

I suspect that your program isn't "stalled" by the cout, but never exits the while loop because every value generated is less than 99999999 + 1...


"is there a way around this?? should I be using a different variable"


If you are on a system with a 16 bit integer, then no - rand returns an integer, so it can't give you bigger numbers rthan 32K.

However, there is nothing stopping you from calling it twice into a long:

C++
long FourDigitRandom()
   {
   return (long) rand() % (9999 + 1); 
   }
...
   long l = FourDigitRandom() + (FourDigitRandom() * 10000);
...
 
Share this answer
 
v2
Comments
Gavon Black 11-May-13 11:06am    
is there a way around this?? should I be using a different variable
OriginalGriff 11-May-13 11:17am    
Answer updated
You need to think more about your code!
1. "rand()" returns a pseudo-random (i.e. any number) between 0 and RAND_MAX (32767). Unless you format the value to 8 digits by zero padding, the output could be any length from 1 to 5 digits.
2. Using mod 99999999 means that you end up with the same value that rand returned, because any value will be less than that, so the modulus (remainder) is always that value.
3. Your while loop is never going to finish, because 'i' can never be 99999999.

To get a pseudo-random value from between two values using rand, you will need to do this:

C++
int value = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
            + range_min;


as given in the example here rand()[^]

In your case, range_max = 99999999 and range_min = 1.

Or, if you want to ensure it is always 8 digits without leading zeros, then range_min = 10000000.

Regards,
Ian.
 
Share this answer
 
What your rand() does is give a number between 0 and 32767. Now, if you want your number to be ALWAYS a 8 digit number, not smaller than 1000000, what you can use as an effective solution is generate each number separately. Also, it depends on your machine if you can store 8 digit numbers. So, if you only want to display, I'd suggest using two 4 digit numbers.

Here's what you can use :

C++
int i = 0,j = 0;

i = rand() % 9 + 1;            //First number should not be 0.

for (int k = 1; k < 4; k++) {
    i = i*10 + rand()%10;      //Generate the next 3 digits.
}

for (int k = 0; k < 4; k++) {
    j = j*10 + rand()%10;      //Generate the 4 digits of the lower part.
}

cout<<"Random number : "<<i<<j;

// If you want the 8 digit number, save in long int
long int res = i*1000 + j;
cout<<res;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900