Reputation: 279
this line printf ("Random[%d]= %u\n", i, rand ());
generates a random integer mostly 1 bit, but when doing printf ("Random[%d]= %u\n", i, rand ()%1000000);
it generates 6 digits int,
when trying to generate a 10 digit int the error " warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘long long int’ [-Wformat]" occurs.
so is there a way to generate a random 10 bit signed integer???
Upvotes: 0
Views: 2581
Reputation: 409166
You would have passed sa "long long" or "unsigned long long" either explicitly or implicitly to the printf, which is why you get the warning -- but more on that later;
Generating a 10 digit random number, you will need to switch to 64 bit -- so long long;
unsigned long long randomvalue;
randomvalue = random();
randomvalue <<= 16; // just picked 16 at random
randomvalue ^= random(); // you could also use + but not "or";
randomvalue %= 10000000000ULL;
The warning you get is because you pass a value of type long long
as the third argument to the printf
function, but you still use "%u"
as the format, which makes printf
expect a value of type unsigned int
.
To silence the warning, change the format to "%lld"
.
It won't help you create a ten-digit number higher than 2147483647
though, as rand
returns an int
which so far is at most 32 bits, even on 64-bit platforms. Where the value 2147483647
comes from? The highest positive value if a signed 32-bit integer.
Upvotes: 1
Reputation: 24875
It does not fit in an int32 (check your platform to see how int
is defined)
In an unsigned int32, the máximum value is 2^32 = 4,294,967,296. So when you do rand % 10000000000
, 10000000000
is a long
so %
is computed as a long
operation (which gives a result of such a type)
Upvotes: 1