filling array with rand numbers

  • Thread starter Bill Cunningham
  • Start date

B

Bill Cunningham

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 };
srand(time(NULL));
r = rand();
for (i = 0; i < 64; i++)
key = r;
printf("%d%d\n", key[0], key[1]);
}
 
Ad

Advertisements

O

osmium

Bill Cunningham said:
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 };
srand(time(NULL));
r = rand();

That's the only random number you draw. Put the draw *in* the loop.
for (i = 0; i < 64; i++)
key = r;
printf("%d%d\n", key[0], key[1]);
}
 
B

Bill Cunningham

osmium said:
Bill Cunningham said:
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 };
srand(time(NULL));
r = rand();

That's the only random number you draw. Put the draw *in* the loop.
for (i = 0; i < 64; i++)
key = r;
printf("%d%d\n", key[0], key[1]);
}


I'm not sure what you mean "the draw".
 
G

Geoff

osmium said:
Bill Cunningham said:
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 };
srand(time(NULL));
r = rand();

That's the only random number you draw. Put the draw *in* the loop.
for (i = 0; i < 64; i++)
key = r;
printf("%d%d\n", key[0], key[1]);
}


I'm not sure what you mean "the draw".


Put the r = rand(); statement inside the for() loop.
 
S

Sjouke Burry

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 };
srand(time(NULL));
r = rand();
for (i = 0; i< 64; i++)
key = r;
printf("%d%d\n", key[0], key[1]);
}


fill the array with 1----n.
Swap each cell with a random cell.
 
K

Keith Thompson

Sjouke Burry said:
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 };
srand(time(NULL));
r = rand();
for (i = 0; i< 64; i++)
key = r;
printf("%d%d\n", key[0], key[1]);
}


fill the array with 1----n.
Swap each cell with a random cell.


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.
 
Ad

Advertisements

M

Malcolm McLean

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.
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%.
 
G

Geoff

Sjouke Burry said:
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 };
srand(time(NULL));
r = rand();
for (i = 0; i< 64; i++)
key = r;
printf("%d%d\n", key[0], key[1]);
}


fill the array with 1----n.
Swap each cell with a random cell.


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.


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.
 
M

Malcolm McLean

On Sun, 04 Aug 2013 19:42:13 -0700, Keith Thompson <[email protected]>


His choice of key[] and txt[] for array names suggests some kind of
cryptographic experiment.
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.
 
B

Ben Bacarisse

Malcolm McLean said:
Its also not the way to write shuffle().
True.

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 a 50% * 50%
chance of swapping a 1 back, and a 50% chance its a 2. therefore a
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%.

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.
 
K

Keith Thompson

Geoff said:
Sjouke Burry said:
On 04.08.13 23:20, Bill Cunningham wrote:
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 };
srand(time(NULL));
r = rand();
for (i = 0; i< 64; i++)
key = r;
printf("%d%d\n", key[0], key[1]);
}

fill the array with 1----n.
Swap each cell with a random cell.


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.


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.


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.
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.

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.

[...]
 
Ad

Advertisements

K

Keith Thompson

Malcolm McLean said:
On Sun, 04 Aug 2013 19:42:13 -0700, Keith Thompson <[email protected]>
His choice of key[] and txt[] for array names suggests some kind of
cryptographic experiment.
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.

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
experts.

"The generation of random numbers is too important to be left to
chance." -- Robert R. Coveyou
 
M

Malcolm McLean

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.
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.
 
M

Malcolm McLean

The common wisdom is that typical implementations of rand() are not at
all suitable for encryption.
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.
 
O

osmium

Robert Wessel said:
On Sun, 04 Aug 2013 19:42:13 -0700, Keith Thompson <[email protected]>


His choice of key[] and txt[] for array names suggests some kind of
cryptographic experiment.
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.


Only for some extremely generous definitions of "reasonable"!

Almost all srand/rand implementations are LCGs, and have very small
seeds ("keys" - typically 32 bits), and brute forcing them is fairly
trivial. Even worse, LCG PRNGs have terrible cryptographic
properties, so brute forcing them is really doing it the hard way.
Usually simple frequency analysis will do the trick.

So that might keep your kid sister out of your diary, but no one else.

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?
 
B

Ben Bacarisse

Robert Wessel said:
Only for some extremely generous definitions of "reasonable"!

Almost all srand/rand implementations are LCGs, and have very small
seeds ("keys" - typically 32 bits), and brute forcing them is fairly
trivial. Even worse, LCG PRNGs have terrible cryptographic
properties, so brute forcing them is really doing it the hard way.
Usually simple frequency analysis will do the trick.

So that might keep your kid sister out of your diary, but no one else.

....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.

<snip>
 
Ad

Advertisements

B

BruceS

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 };
srand(time(NULL));
r = rand();
for (i = 0; i< 64; i++)
key = r;
printf("%d%d\n", key[0], key[1]);
}


fill the array with 1----n.
Swap each cell with a random cell.


Am I really the only one here who thought Sjouke was trolling the troll?
 
O

osmium

BruceS said:
Am I really the only one here who thought Sjouke was trolling the troll?

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.
 
K

Keith Thompson

Malcolm McLean said:
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.

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.
 
Ad

Advertisements

J

James Kuyper

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.

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
success.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top