How to have a random number that is always 7 numbers long

ok so i have a random number generator and i was wondering how to make it so i always have 7 or another certain amount of numbers that are random, how would i do that?

srand(time(0));

int number;

???
how many "digits long" a number is is just a matter of output formatting. So "0" is a 7 digit number if you print it as "0000000"

What you probably meant is you want numbers within a fixed range. That is.. you want numbers between [1000000,10000000)

In which case the "usual" formula for generating random numbers in a range applies:

1
2
3
4
5
6
7
8
9
10
11
12
int rand(int min, int max)  // [min,max) .. ie, max is exclusive
{
    int range = max-min;
    return (rand() % range) + min;
}

int main()
{
    srand(...);

    int a_7_digit_number = rand( 1000000, 10000000 );
}


EDIT: note this assumes RAND_MAX is large enough. If you're using C++11 you'd be better off using the <random> header, rather than rand().
Last edited on
closed account (28poGNh0)
can you give us some examples
I want the number generated to be lets say 7 digits long each and every time, but each digit is random

9572510
8400932
7900012
Last edited on
closed account (28poGNh0)
like this ?

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
# include <iostream>
# include <ctime>
# include <cstdlib>
# include <cmath>
using namespace std;

int getRandNbr(int amount)
{
    bool pass = true;
    int nbr;

    while(pass)
    {
        nbr = rand()*rand();
        if(ceil(log10(nbr))==amount)
            break;
    }return nbr;
}

int main()
{
    srand(time(NULL));

    for(int i=0;i<10;i++)
        cout << getRandNbr(7) << endl;
    return 0;
}

is there any method that requires less code?
closed account (28poGNh0)
also take a look of @Disch 's function's implementation it is a good function
Last edited on
I put my example in a separate function simply because that helps clarify what it's doing.

If that's "too much code" then you can put it all on one line:

 
int number = (rand() % 9000000) + 1000000;
I did this and it works is this good?

1
2
3
4
5
6
7
8
9
vector <int> randNumbers;
srand(time(0));

for(int i = 0; i < 7; i++)
{
    int local_randNumber = rand() % 10;
    randNumbers.push_back(local_randNumber);
    cout << randNumbers[i];
}


your answers helped give me the idea.
Might as well use a std::string.
1
2
3
4
5
std::string ID("");
for(int i = 0; i < 7; i++)
{
    ID += '0' + rand()%10;
 }
why use a string instead? also i get tons of numbers and they arent random.
Last edited on
Did you just copy/paste my snippet? I didn't put srand() in it.
Also, strings, so you can do stuff like:
1
2
3
cout << ID;
if(ID1 < ID2)
//... 

A lot easier. I think in another thread, you had a class that had something like an ID variable? That's what I'm assuming this randomizing stuff is for.
oh, well yeah i did but i had srand() in there and it still didnt work.

1
2
3
4
5
6
7
8
    srand(time(0));

    std::string ID("");
    for(int i = 0; i < 7; i++)
    {
        ID += '0' + rand() % 10;
        cout << ID;
    }
You are printing the ID as it forms all clumped up.
1
2
3
4
5
6
7
8
9
for(/*...*/){
//...
    cout << ID << ' ';
}

//or

for(/*...*/){/*...*/}
cout << ID;
oh, sorry thats right its not like the vector. ok it works now, thanks.
I think he means a 7 digit id as in 1234567 which would be next to each other since he is generating a random number 0-9 one at a time.
Topic archived. No new replies allowed.