Hey everyone

I was working on a small tool and also refreshing my programming skills. my problem is I want to pick a random number from 60,000 to 200,000. it does that but the number it displays are not dramaticly changed. For example if it out puts 062045, i hit enter to do my while loop to reset it again and it moves up to like 062049....

i want it to go like if at one point its at lets say 62353, then i press enter to run again my while loop and it should change dramatically and maybe go to like in the 100,000 range. I am not forcing it but the number is changing very slowly. at the moment.

here is my code

``````void NNNNNN_algo()
{
srand((unsigned)time(0));
int min = 60000;
int max = 180000;
int number = min + rand() % max;
if(number < 100000)
cout << "NNNNNN: " << "0" << number << endl;
else
cout << "NNNNNN: " << number << endl;

}
``````

rand() does not have the range necessary. It only generates a random number between 0 and RAND_MAX (just output that number to see the necessary range). As such you have three options:

1) Write your own pseudo random number generator (wikipedia has some good articlos on them)

The reason is because you should not re-seed the random number generator each time. Just seed it with `srand((unsigned)time(0));` once in the entire application, i.e., at the start of the main() function. That will fix the problem.

BTW, to print a fixed number of digits with leading zeros, you …

## All 5 Replies

rand() does not have the range necessary. It only generates a random number between 0 and RAND_MAX (just output that number to see the necessary range). As such you have three options:

1) Write your own pseudo random number generator (wikipedia has some good articlos on them)

3) Combine multiple calls to rand() (you could add them to a larger data type then do your standard operations, or come up with a less biased technique)

Just an aside, rand() is fairly random, but using modulo (%) to limit it will cause it to tend towards lower numbers. This is because while % works fine for most of the range, unless you % it by a number that it is perfectly divisible by, there will be a higher chance of it generating the lower numbers than the higher ones. EG if it generates a number from 1-10 then %2 works fine (5 chances to return 1, 5 to return 0), but %3 doesnt (3 chances to return 2, 3 chances to return 0, but 4 chances to return 1 :O)

The reason is because you should not re-seed the random number generator each time. Just seed it with `srand((unsigned)time(0));` once in the entire application, i.e., at the start of the main() function. That will fix the problem.

BTW, to print a fixed number of digits with leading zeros, you can just use the following:

``````cout << "NNNNNN: " << setfill('0') << setw(6) << number << endl;
``````

I am not much of an advance programmer lol but I will read it for self interest.

@mike 2000 17, I was using functions so if any of my functions need rand i put the seed in side the function. the int main is only calling the function.

You could still keep the seeding from reoccurring like:

``````static bool seeded = false;
if( !seeded )
{
srand( unsigned ( time( NULL ) ) );
seeded = true;
}
``````

i put the seed in side the function. the int main is only calling the function.

``````void my_function()
{
static const int seed_once = ( std::srand( std::time(0) ), 0 ) ;
// ...
}
``````

I want to pick a random number from 60,000 to 200,000.

``````#include <iostream>
#include <random>
#include <chrono>

int NNNNNN_algo( int min = 60000, int max = 180000 )
{
// if the implementation has a random device
// static std::random_device rdev ; static std::mt19937 rng( rdev() ) ;

// else
static std::mt19937 rng(
std::chrono::high_resolution_clock::now().time_since_epoch().count() ) ;

return std::uniform_int_distribution<int>( min, max )( rng ) ;
}

int main()
{
while( std::cin.get() ) std::cout << NNNNNN_algo() << '\n' ;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.