filling array with rand numbers

Discussion in 'C Programming' started by Bill Cunningham, Aug 4, 2013.

  1. I have this code and it is iterating but I think I somehow somewhere
    need another loop. I can get random numbers with this code but I think all
    members of the array are the same random number. I want all 64 filled with a
    different random number. What am I needing?

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main()
    int i, r;
    char key[64];
    char txt[64] = { 0 };
    r = rand();
    for (i = 0; i < 64; i++)
    key = r;
    printf("%d%d\n", key[0], key[1]);
    Bill Cunningham, Aug 4, 2013
    1. Advertisements

  2. Bill Cunningham

    osmium Guest

    That's the only random number you draw. Put the draw *in* the loop.
    osmium, Aug 4, 2013
    1. Advertisements

  3. I'm not sure what you mean "the draw".
    Bill Cunningham, Aug 4, 2013
  4. Bill Cunningham

    Geoff Guest

    Put the r = rand(); statement inside the for() loop.
    Geoff, Aug 5, 2013
  5. Bill Cunningham

    Sjouke Burry Guest

    fill the array with 1----n.
    Swap each cell with a random cell.
    Sjouke Burry, Aug 5, 2013

  6. That doesn't do the same thing. If corrected, Bill's program
    would set each element to a random integer from 0 to RAND_MAX.
    Your suggestion would set each element to a value from 1 to n with
    no repetitions.
    Keith Thompson, Aug 5, 2013
  7. Its also not the way to write shuffle(). Consider shuffling an array of two,
    starting with the sequence 1, 2. If we swap the 1 with a random element,
    it has a 50% chance of remaining 1, which is correct. If we then swap the two position with a random element, there a 50% chance it's a 1, therefore a50% * 50% chance of swapping a 1 back, and a 50% chance its a 2. thereforea 50*50 chance of swapping a 2 back. So p(1 at one) = 50% * 50% (no swaps) + 50% * 50% * 50% (swap the 1 to the two position, then swap back). That's 25% + 12.5%, or 37.5%, which is wrong. p(1 at one) should be 50%.
    Malcolm McLean, Aug 5, 2013
  8. Bill Cunningham

    Geoff Guest

    Bill's program won't set the elements to a random integer from 0 to
    RAND_MAX. It will set the elements of the array to the lower
    sizeof(char) bits of the random numbers generated between 0 and
    RAND_MAX. But only if he writes his for() loop correctly.

    As it is, it fails to initialize the key[] array properly and his
    output statement doesn't do anything but output the first two members
    of the array.

    His choice of key[] and txt[] for array names suggests some kind of
    cryptographic experiment.
    Geoff, Aug 5, 2013
  9. It's possible to write a reasonable-strength encryption program with srand(), rand and the exclusive or operator. But it's better to implement your own rand() to ensure portability.
    Malcolm McLean, Aug 5, 2013
  10. But it works for two-element arrays (although it wastes a random
    sample). The first swap produces either 1,2 or 2,1 with equal
    probability. The second produces either 2,1 or 1,2 from the first
    possibility and either 1,2 or 2,1 from the second. Both permutations
    occur with equal probability among the four outcomes.

    A counting argument also shows your numbers can't be right. The
    algorithm makes two binary choices ending up with four outcomes. These
    will have to be split 2-2, 1-3 or 0-4 between the two possible
    permutations. Even if the algorithm did favour one outcome over the
    other, it would have to do it 1-3 or 0-4.

    For three elements, the algorithm makes three three-way choices to give
    27 answers. These can't be split evenly between the six permutations,
    so we can conclude, without further ado, that it fails for n=3.
    Ben Bacarisse, Aug 5, 2013

  11. Actually, it will set each element to the result of converting the
    pseudo-random number to type char, which is implementation-defined if
    plain char is signed. But yes, it's likely to be the low-order CHAR_BIT
    (not sizeof(char)) bits.
    With no space between them, so you can't tell the difference between
    {12, 34} and {1, 234} -- but I'm not trying to fix the code.

    Keith Thompson, Aug 5, 2013
  12. The common wisdom is that typical implementations of rand() are not at
    all suitable for encryption.

    I'd agree with the "implement your own" advice if you mean to copy an
    existing well tested PRNG. If you really mean implementing your own
    from scratch, that's a bad idea unless you're an expert, or a team of

    "The generation of random numbers is too important to be left to
    chance." -- Robert R. Coveyou
    Keith Thompson, Aug 5, 2013
  13. You're right. I put in an extra factor of 50% (chance that the swapped element
    is a 2 or a 1). But if the first swap took place, it must be a 1. So it's
    50% * 50% + 50% * 50% = 50%, and correct for the 2 case. You need to go to three to show the problem.
    Malcolm McLean, Aug 5, 2013
  14. Depends on the identity of Eve. If you're a person of interest to GCHQ,
    then you can be sure that a skilled programmer will be assigned to your case,
    and pretty much the first thing he'll do is try the standard Unix implementation of rand() with every seed.
    But if you're just a regular peer to peer music pirate, the music companies
    can't afford to employ programmers to break encryption, because it costs
    maybe an hour's work per case, and there are millions of people
    to monitor. But they can afford to put up a simple sniffer that looks for
    the digital signature of their MPEG. By imposing a cost on them, you direct
    them to easier legal targets elsewhere.
    Malcolm McLean, Aug 5, 2013
  15. Bill Cunningham

    osmium Guest

    Let's focus on that brute force approach for a minute. One result is
    atackatdawn another result is sueforpeace. Which result do you choose as
    the proper "cracking" of the code? Or does brute force mean something else?
    osmium, Aug 5, 2013
  16. ....yeah your kid brother would crack it right away.

    On the other hand, had I been Barbara Liskov's older brother, I don't
    think I'd have trusted my secrets to an LCG-based cipher.

    Ben Bacarisse, Aug 5, 2013
  17. Bill Cunningham

    BruceS Guest

    Am I really the only one here who thought Sjouke was trolling the troll?
    BruceS, Aug 5, 2013
  18. Bill Cunningham

    osmium Guest

    It looked like a normal Usenet digression to me. Ask a question, get six
    responses: do it this horribly convoluted way instead, you can't do that in
    standard C, your mother is a whore, why are you doing this, you're a troll,
    what misbegotten book are you using. And maybe one on topic answer.

    And I don't agree that Bill is a troll.
    osmium, Aug 5, 2013
  19. Eve? GCHQ? I have no idea what those refer to. Are they topical?

    High-quality encryption software is readily available, and it's easier
    to use it than to roll your own low-quality encryption software.

    I suppose music "piracy" is one possible application of encryption, but
    it's not what I had in mind.
    Keith Thompson, Aug 5, 2013
  20. Bill Cunningham

    James Kuyper Guest

    Can you really imagine someone spending more than a dozen years learning
    C, and not having learned any more about it than Bill does? I have no
    difficulty imagining that someone might have that much difficulty
    learning C - I've met many people like that. What I find hard to imagine
    that such a person would keep trying for so long despite their lack of
    James Kuyper, Aug 5, 2013
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.