Hi,
How can we generate 16 digit random nos in C.
Hi,
How can we generate 16 digit random nos in C.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int n;
srand(time(NULL));
for(n=0; n<4; n++) printf("%04d", rand()%10000);
}
see also lrand48()
But. None of these are going to give you 16 digit random numbers. They are not meant for that. Corona's idea of concatenating output from a PNG is about as good as it gets.
Note that it will cycle (or repeat) 4 times faster because you are using four calls to a PNG to get one number, ie your sequence will start to become non random after a few thousand calls. ie., approximately (RAND_MAX /2) / 4 = ~4096 numbers
If you want something better try the GSL - gnu scientific library
GSL - GNU Scientific Library - GNU Project - Free Software Foundation (FSF)
thnks for reply...
But my requirement is like ,we r designing a product that will generate a unique 16 digit no for every call.
Does my solution work, or do you have different requirements? Note that my solution will return the same value if called more than once a second!
Another thought - not all unixes have /dev/urandom and /dev/random.
You can read abou them, but they do provide a random bitstream. If your box has them (Soalris & Linux come to mind) read on -
An example of how to play with the bitstream using Corona's idea.:
#include <stdio.h>
typedef
union
{
char tmp[sizeof(unsigned long long)];
unsigned long long myll;
} ll_t;
unsigned long long llrand(void)
{
FILE *in=fopen("/dev/urandom", "r");
ll_t ll_u;
fread(ll_u.tmp, sizeof(ll_u.tmp), 1, in);
fclose(in);
return ll_u.myll;
}
int main()
{
char tmp[64]={0x0};
char working[64]={0x0};
int i=0;
for(i=0; i< 20; i++)
{
while(strlen(tmp) < 16)
{
sprintf(working, "%lu", llrand() );
strcat(tmp, working);
}
tmp[16]=0x0;
printf("%s\n", tmp);
*tmp=0x0;
}
return 0;
}
This needs work but you get the idea.
thanks jim...
its generating 16 digit no, but could please tell me that how the nos can be generated in double format.
i tried using atof() , but did nt work.
There is a reason - most systems double datatype has 15 digits of precision.
see limits.h DBL_DIG
Try long double instead. I also thought you needed 16 digits, not a numeric datatype like double.
For example, suppose you get 0000000123456789 from /dev/urandom. If that becomes a numeric datatype it no longer has 16 digits. It has only nine digits.
From man pages, random seem to be more suitable for key generation than urandom character stream
I should add that a floating point datatype is the wrong kind of variable to hold integer data. Even a very high precision floating point value does not have complete precision. a 64-bit 'long long' type seems better suited. Or better yet, include stdint.h and use int64_t. To convert from a string, see atoll.
/dev/random has the problem that sometimes it may block. When there's not enough entropy available the read() call will wait until there is. crypto-grade randomness may not be needed unless this really is for crypto.
Random is a loaded word. If you want somewhat random numbers, use the system call 'rand'.
If you need something more rigorous, you need to look into encryption software. Just feed any robust encryption engine a text string and you will get a random sequence of bits back that you can slice and dice as you like. Feed it the same string concateneated to itself, you will still get a random stream of non-repeating bits. Change the key and you get a different random stream of bits.
Google 'aes encryption source code' [AES stands for advanced encryption standard] There are other packages, but that is the standard.
You can do cool graphics stuff with true random numbers. Quasi-random numbers quickly reveal visual artifacts.