vendredi 4 décembre 2015

How to generate random numbers in C two colons?

I need in c code that generates two numbers in horizontally...so that i can get token numbers for my login system.

I need that i get this:

token=0.152644,0.429187

so in example i have token= and random generated numbers that have at beginning 0. and then 6 random generated numbers separated with , sign. How to get get this in C?

I have try this code but it does not give me what i want_

#include <stdio.h>
#include <string.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 tmp1[64]={0x0};
    char working[64]={0x0};
    int i=0;

    for(i=0; i< 1; i++)
    {
        while(strlen(tmp1) < 6)
        {
            sprintf(working, "%lu", llrand() );
            strcat(tmp1, working);
        }
        tmp1[6]=0x0;

        printf("%s\n", tmp1);
        *tmp1=0x0;
     }
    return 0;
 }

From output i get this:

747563
102595

Can code be simple and short?




Aucun commentaire:

Enregistrer un commentaire