Click here to Skip to main content
13,198,465 members (61,315 online)
Rate this:
Please Sign up or sign in to vote.
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.

          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 11-May-13 4:49am
Updated 11-May-13 4:55am
ThePhantomUpvoter 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
ThePhantomUpvoter 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

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
Gavon Black 11-May-13 11:23am
but in essence that is just giving me 8 random numbers correct?? how do I in turn make that one number that I can use later on in my program??
enhzflep 11-May-13 13:13pm
I assume this was intended for me (hit the "<- reply" button to the right of a person's comment, or the "Have a question or Comment" button below a solution or question.

Not quite - it uses rand 8 times to generate each number. The number is accumalated in the var result.

The stdlib implementation of rand is known as being imperfect in terms of the distribution of the results returned. It is also (like all 'random' number generators) periodic. Using the function 8 times to generate 1 number will mean it repeats 8 times sooner than it would ordinarily. If you look closely, you'll see that I run the code to generate an 8 digit number - 10 times. Simple, just run it once and use the value held in result as your er, result. Presumably you can identify how the code could be put into it's own function - one that would allow you to specify the maximum number of digits. Remember, with a 32 bit var storing the result, your largest number is 4 point something billion, which would mean that the largest value you could substitute for the 8 is 9 (i.e 9 base-10 digits could be as large as 999 million) To add an extra digit, you'd have to add logic to check the values of the individual digits.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:

long FourDigitRandom()
   return (long) rand() % (9999 + 1); 
   long l = FourDigitRandom() + (FourDigitRandom() * 10000);
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:

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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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 :

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;

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171020.1 | Last Updated 13 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100