I am using CodeBlocks c++ compiler.
I want to generate 11 digit random number as i am working on my computer project.
How can i do this? and everytime a new random number.

All 13 Replies

A little searching will always help. There is already a thread for this.

Just use the code it shows there for generating a random number between a range, and use a range of 10000000000 to 99999999999.

1) Seed random number
2) use rand()/RAND_MAX * (max-min) + min;

1) Seed random number
2) use rand()/RAND_MAX * (max-min) + min;

A little searching will always help. There is already a thread for this.

Just use the code it shows there for generating a random number between a range, and use a range of 10000000000 to 99999999999.

1) Seed random number
2) use rand()/RAND_MAX * (max-min) + min;

On 32 bit systems int is usually capped somewhere around 2,147,483,647. Visual Studio is at least one popular compiler that caps RAND_MAX at the minimum value of 32,767 even on 32 bit systems. For either of those suggestions to work, you would have to be on a 64 bit system with RAND_MAX being large enough to handle the upper limit of the range.

himgar, you probably need to look for a 64 bit random number generator. I do not know if gcc has one, but that is the default compiler that comes with Code::Blocks, so it should help you get started. You can use the long long data type for gcc to hold the value once it is generated.

outputs number from 10000000000 to 99999999999

``````#include<iostream>
#include<ctime>

using namespace std;

typedef unsigned long long Type;

Type myRand(const Type Max, const Type Min = 0)
{
return rand()/(float)RAND_MAX * (Max-Min) + Min ;
}

int main()
{
srand(unsigned(time(0)));

const Type MAX = 99999999999;
const Type MIN = 10000000000;

for(int i = 0; i < 12; i++)
{
if(i && i % 4 == 0)
cout<<endl;

cout.width(10);
cout<<myRand(MAX,MIN)<<" ";
}

cout<<"\n\n";

}``````

outputs number from 10000000000 to 99999999999

If you are happy with that result, then OK. But do not expect the same quality of random numbers that a 64 bit generator would have. All you are doing is taking the small period of rand and throwing it into a larger type. The period is not changing, and the distribution is probably going to be much worse.

If you are happy with that result, then OK. But do not expect the same quality of random numbers that a 64 bit generator would have. All you are doing is taking the small period of rand and throwing it into a larger type. The period is not changing, and the distribution is probably going to be much worse.

Maybe but it is probably good enough for this experiment. And if you wanted better distribution then go with
something like me Mersenne Twister.

Maybe but it is probably good enough for this experiment.

If you have to say 'probably', it is not a safe assumption to make. ;)

If you have to say 'probably', it is not a safe assumption to make. ;)

Well, lets ask. To the OP is the above code Good enough for
your experiment? And by the way, when can one be sure about
anything, except math. Isn't everything based on assumption?
Shout outs to Descartes.

@OP : Just another idea. Generate some random numbers and
add it into a string, making sure that the strings lengths is in the desired
range. And the convert it back into the desired data type.

And by the way, when can one be sure about
anything, except math. Isn't everything based on assumption?

I prefer to err on the side of caution. If I have to make an assumption, it is an assumption that things will be worse than expected, not better. Experience has proven that to be a good assumption. :)

I prefer to err on the side of caution. If I have to make an assumption, it is an assumption that things will be worse than expected, not better. Experience has proven that to be a good assumption. :)

Yep, experience does help. But either way its still an assumption.

How about creating an array[11] and randomize each element in a loop??

Be a part of the DaniWeb community

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