Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: C++, Help

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    C++, Help

    I would like to know that is it possible to generate a list of 16 digit random numbers in C or C++, if it is, then how…..


  2. #2
    Webius Designerous Indiginous
    Join Date
    Mar 2002
    Location
    South Florida
    Posts
    1,123
    Sure its possible, I'll provide a program to do it, but since I don't have a compiler to test it, it may need a bit tweaking here and there.

    #include<iostream.h>
    #include<stdlib.h>

    void main(void)
    {
    int a, b, cC, d, e, f, g, h, i, j, k, l, m, n, o, p, x;

    cout<<"How many outputs would you like?">>endl;
    cin>>x;

    a = 1 + rand() % 9;
    b = 1 + rand() % 9;
    cC = 1 + rand() % 9;
    d = 1 + rand() % 9;
    e = 1 + rand() % 9;
    f = 1 + rand() % 9;
    g = 1 + rand() % 9;
    h = 1 + rand() % 9;
    i = 1 + rand() % 9;
    j = 1 + rand() % 9;
    k = 1 + rand() % 9;
    l = 1 + rand() % 9;
    m = 1 + rand() % 9;
    n = 1 + rand() % 9;
    o = 1 + rand() % 9;
    p = 1 + rand() % 9;

    for(int c = 0; c <= x; c++)
    cout<<a<<b<<cC<<d<<e<<f<<g<h<<i<<j<<k<<l<<m<<n<<o<<p<<endl;

    }


    That may not be pretty, but it should work.
    Someone be kind enough to compile and try that. Try not to rip my code apart too much. :P


    Well, it seems after some testing from vicTT, its pretty buggy, I'm gunna write it using an array and repost it in this post when finished. heh

  3. #3
    () \/V |\| 3 |) |3\/ |\|3G47|\/3
    Join Date
    Sep 2002
    Posts
    744
    Hey guys, I compiled it and fixed a few VERY minor errors. This should work.


    include<iostream.h>
    #include<stdlib.h>

    void main(void)
    {
    int a, b, cC, d, e, f, g, h, i, j, k, l, m, n, o, p, x;

    cout << "How many outputs would you like?"<< endl;
    cin>>x;

    a = 1 + rand() % 9;
    b = 1 + rand() % 9;
    cC = 1 + rand() % 9;
    d = 1 + rand() % 9;
    e = 1 + rand() % 9;
    f = 1 + rand() % 9;
    g = 1 + rand() % 9;
    h = 1 + rand() % 9;
    i = 1 + rand() % 9;
    j = 1 + rand() % 9;
    k = 1 + rand() % 9;
    l = 1 + rand() % 9;
    m = 1 + rand() % 9;
    n = 1 + rand() % 9;
    o = 1 + rand() % 9;
    p = 1 + rand() % 9;

    for(int c = 0; c <= x; c++)
    cout << a << b << cC << d << e << f << g<< h << i << j << k << l << m << n << o << p << endl;

    }

    Go Finland!
    Deviant Gallery

  4. #4
    Umm, specificly 16 digits. Lets see:

    In C/C++, you can use the rand() or the srand() functions. They won't give you 16 digit random numbers, you'll have to give it any algorithm you like.. for example:

    randomnumber1 = srand() * 91234567890;
    randomnumber2 = srand() * 99234567890
    .
    .
    .


    Basically, all you're doing here is generating a random number, using the 'srand()' function, and then multiplying it with a sufficiently large number. The number, ofcourse, could be anything at all. Just make sure its big. Real big.

    That should be one way of doing it. There might be better ways though. heh. ( for example, xmadness's way. His loop for printing the list is fine, so use that. Just replace the 1 + rand... part with the line I put above. Try both methods, and let me know if 1 + rand() % 9 gives such a big number. )

    As an afterthought, can a 16 digit number be defined as an integer? I mean, won't it need more space than is reserved for the 'int' type? Just wondering.

    And, on a totally unrelated note, a variable in C/C++ can have a really really huge name. I actually created a variable with over 1000 characters, and the complier ( in this case Borland C++ 5 ) gave me no problem. heh. Just something to share.

    [pong]Cheers.[/pong]

    Ah hell, if that code works that sweet. Use that.
    Thanks, mathgirl, for clearing that up.
    I blame you cos my mind is not my own, so don't blame me if I trespass in your zone!

  5. #5
    () \/V |\| 3 |) |3\/ |\|3G47|\/3
    Join Date
    Sep 2002
    Posts
    744
    Well...testing doesn't give quite the right output.

    Go Finland!
    Deviant Gallery

  6. #6
    Webius Designerous Indiginous
    Join Date
    Mar 2002
    Location
    South Florida
    Posts
    1,123
    Originally posted here by RejectKnowledge

    [b]In C/C++, you can use the rand() or the srand() functions. They won't give you 16 digit random numbers, you'll have to give it any algorithm you like.. for example:

    randomnumber1 = srand() * 91234567890;
    randomnumber2 = srand() * 99234567890
    .
    .
    .

    Yeah your right, using the rand() will always produce the same output each time the program is run. To prevent that you can use the srand(), which requires a pre inputted number to run the algorithm. That way it has a different starting position in the algorithm. Its called a seed value.


    As an afterthought, can a 16 digit number be defined as an integer? I mean, won't it need more space than is reserved for the 'int' type? Just wondering.
    Well they way I wrote it, there is only a single value, 0-9, that will be stored in each variable, so in essence, I could have, and should have, use a short variable type. And I prolly should have used an array also.

    short num[16];


    Heh, gotta love programming, 1000 different ways to do the same thing.

  7. #7
    My piece of code will generate any amount of big numbers, because of the multiplication. madness on the other hand is printing out 16 individual random integers. Therefore the loop for using my code will be more alng the lines of:

    cout << "How many outputs would you like?"<< endl;
    cin>>x;

    for ( int c = 0; c < x; c++){
    randomnumber = srand() * 99234567890;
    cout << randomnumber << endl;
    }

    There. hehe.


    Yep. The beauty of programming. Did you check out the thread about the completition of freaky coding? heh. It's a riot. And yes, using an array would've been more efficient.

    and mathgirl, did you test my code, or is madness' not giving the right output?
    I blame you cos my mind is not my own, so don't blame me if I trespass in your zone!

  8. #8
    Webius Designerous Indiginous
    Join Date
    Mar 2002
    Location
    South Florida
    Posts
    1,123
    haha, so what is the correct program? Heh, see what happens when you don't have a compiler in front of you?

    #include<iostream.h>
    #include<stdlib.h>

    void main(void)
    {
    int a, b, cC, d, e, f, g, h, i, j, k, l, m, n, o, p, x;

    cout<<"How many outputs would you like?"<<endl;
    cin>>x;


    for(int c = 0; c < x; c++)
    {

    a = 1 + rand() % 9;
    b = 1 + rand() % 9;
    cC = 1 + rand() % 9;
    d = 1 + rand() % 9;
    e = 1 + rand() % 9;
    f = 1 + rand() % 9;
    g = 1 + rand() % 9;
    h = 1 + rand() % 9;
    i = 1 + rand() % 9;
    j = 1 + rand() % 9;
    k = 1 + rand() % 9;
    l = 1 + rand() % 9;
    m = 1 + rand() % 9;
    n = 1 + rand() % 9;
    o = 1 + rand() % 9;
    p = 1 + rand() % 9;

    cout<<a<<b<<cC<<d<<e<<f<<g<<h<<i<<j<<k<<l<<m<<n<<o<<p<<endl;

    }

    cout<<"I have returned "<<x<<" results."<<endl;
    }


    Okay, that should work.. I think. LOL

  9. #9
    () \/V |\| 3 |) |3\/ |\|3G47|\/3
    Join Date
    Sep 2002
    Posts
    744
    !!! This is all so exciting! But I have to go work on "my" programming, now. I know we can all go out and cheat by getting free code for a random number generator off the net, but that wouldn't be very much fun, now, would it??? I started revising what you had, rejeckknowledge, but after I fixed a few things...added some libraries, etc. it was getting a little more involved than I have time for at the moment. I'll check back later tonight and see what's up.

    Good luck.

    Go Finland!
    Deviant Gallery

  10. #10
    Webius Designerous Indiginous
    Join Date
    Mar 2002
    Location
    South Florida
    Posts
    1,123
    Hmm, now my next question. What is this for? If I'm mistaken, It would be a credit card number generator. Tell me thats not what its used for...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •