Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a gift for the mortensens

Reply
Thread Tools

a gift for the mortensens

 
 
frank
Guest
Posts: n/a
 
      01-10-2010
Christmas persists for me. If you ever get an opportunity to attend a
"white elephant party," do so, because it's hilarious.

My friend Alan Mortensen forbade me from spending money on gifts as I
visited his and my family in Salt lake over the actual holidays, so I'm
working out a way where I can give his girls something of value.

I intend to write a couple C utilities for them, as software is not
dough. Where I'm stuck right now is that I can't seem to find source
for invoking pseudo-random behaviour. So here's my first attempt:

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

int main (void)
{

void srand(unsigned seed);
int rand(void);
time_t time(something);

srand(time(&timer));

return("pretty pathetic");
}

I've written hundreds of programs in C that invoke pseudo-random
behavior but nothing recently, or as "frank."

So, I'm looking for a link or some tips on how to invoke pseudo-random
behavior in C. Thanks for your comment.
--
frank
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      01-10-2010
On 10 Jan, 07:35, frank <fr...@example.invalid> wrote:

> I intend to write a couple C utilities [...]. *
> Where I'm stuck right now is that I can't seem to find source
> for invoking pseudo-random behaviour. *So here's my first attempt:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main (void)
> {
>
> void srand(unsigned seed);
> int rand(void);


don't do this. It isn't necessary if you have the approriate #includes


> time_t time(something);
>
> srand(time(&timer));
>
> return("pretty pathetic");
>
> }
>
> I've written hundreds of programs in C that invoke pseudo-random
> behavior but nothing recently, or as "frank."
>
> So, I'm looking for a link or some tips on how to invoke pseudo-random
> behavior in C. *Thanks for your comment.


try the FAQ http://c-faq.com/lib/index.html
particularly questions 13.15 and 13.16

 
Reply With Quote
 
 
 
 
frank
Guest
Posts: n/a
 
      01-10-2010
Nick Keighley wrote:
> On 10 Jan, 07:35, frank <fr...@example.invalid> wrote:
>
>> I intend to write a couple C utilities [...].
>> Where I'm stuck right now is that I can't seem to find source
>> for invoking pseudo-random behaviour. So here's my first attempt:
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main (void)
>> {
>>
>> void srand(unsigned seed);
>> int rand(void);

>
> don't do this. It isn't necessary if you have the approriate #includes


right. They're already declared in the headers.

> try the FAQ http://c-faq.com/lib/index.html
> particularly questions 13.15 and 13.16
>


The FAQ wouldn't load but I have a hard copy here that gave me what
seems to be an appropriate seed:

dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra mort1.c -o out; ./out
i is 1337295409
i is 2147483647
c is 1
dan@dan-desktop:~/source$ ./out
i is 1660238507
i is 2147483647
c is �
dan@dan-desktop:~/source$ cat mort1.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{


int i;
char c;
srand((unsigned int)time((time_t *)NULL));

i=rand();
printf("i is %d\n", i);
printf("i is %d\n", RAND_MAX);
c=(char)i;
printf("c is %c\n", c);

return 0;
}
// gcc -std=c99 -Wall -Wextra mort1.c -o out; ./out
dan@dan-desktop:~/source$

What I want to do now is produce equiprobable chars. If I wanted all
the chars, then I think that the above would suffice, but I want only
lower case letters.

I could use j = rand()%26 or some such and add that character to a, but
I want to be mindful of 13.16, which warns against this. Can someone
say a few words about this?

TIA.
--
frank



 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      01-10-2010
On Sun, 10 Jan 2010 16:05:14 -0700, frank <>
wrote:

snip

>dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra mort1.c -o out; ./out
>i is 1337295409
>i is 2147483647


One of these statements must be false.

>c is 1


While that is the character representation of low order byte on an
ASCII machine, my EBCDIC system will produce significantly different
output.

>dan@dan-desktop:~/source$ ./out
>i is 1660238507
>i is 2147483647
>c is ?
>dan@dan-desktop:~/source$ cat mort1.c
>#include <stdio.h>
>#include <stdlib.h>
>#include <time.h>
>
>int main(void)
>{
>
>
>int i;
>char c;
>srand((unsigned int)time((time_t *)NULL));
>
>i=rand();
>printf("i is %d\n", i);
>printf("i is %d\n", RAND_MAX);


This statement is rarely true. For some seeds, it need not ever be
true.

>c=(char)i;


Does your system complain without the cast? If this code is executed
on a system where char is signed, the cast may not produce the desired
value and may not produce any value.

>printf("c is %c\n", c);
>
>return 0;
>}
>// gcc -std=c99 -Wall -Wextra mort1.c -o out; ./out
>dan@dan-desktop:~/source$
>
>What I want to do now is produce equiprobable chars. If I wanted all
>the chars, then I think that the above would suffice, but I want only
>lower case letters.
>
>I could use j = rand()%26 or some such and add that character to a, but
>I want to be mindful of 13.16, which warns against this. Can someone
>say a few words about this?


There is no requirement for rand to produce random numbers with a
uniform distribution. How will your code handle a normal or Poisson
distribution?

--
Remove del for email
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-10-2010
frank <> writes:
<snip>
> What I want to do now is produce equiprobable chars. If I wanted all
> the chars, then I think that the above would suffice, but I want only
> lower case letters.
>
> I could use j = rand()%26 or some such and add that character to a,
> but I want to be mindful of 13.16, which warns against this. Can
> someone say a few words about this?


When RAND_MAX is 2147483647 (as in your example) rand() results from 0
up to 2147483623 give you remainders 0 to 25 repeatedly. In fact
there are 82595524 sets of remainders. The 24 "left over" numbers,
2147483624 to 2147483647, give remainders 0 to 23 so, on average, 24
and 25 occur very slightly less often (1 in 82595525 times). The bias
is tiny -- will it matter?

If it does matter, instead of calling rand() once you must loop until
the result is between 0 and 2147483623. To be portable you'd write:

int r;
while ((r = rand()) >= RAND_MAX/26);

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-11-2010
Barry Schwarz <> writes:
[...]
> There is no requirement for rand to produce random numbers with a
> uniform distribution. How will your code handle a normal or Poisson
> distribution?


The standard's requirement is (C99 7.20.2.1p2):

The rand function computes a sequence of pseudo-random integers in
the range 0 to RAND_MAX.

I've always assumed that this is meant to imply a uniform
distribution. The authors probably thought that was so obvious it
didn't need to be stated.

Of course, any finite sequence from a uniform distribution has a
finite probability of looking like a normal or Poisson distribution,
or even a sequence of 0s.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-11-2010
frank <> writes:
[..]
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(void)
> {
>
>
> int i;
> char c;
> srand((unsigned int)time((time_t *)NULL));
>
> i=rand();
> printf("i is %d\n", i);
> printf("i is %d\n", RAND_MAX);
> c=(char)i;
> printf("c is %c\n", c);
>
> return 0;
> }


Indentation?

None of the three casts in your program are necessary, and IMHO your
code would be improved by dropping them.

srand(time(NULL);;
...
c = i;

The second "i is %d\n" presumably is a typo for "RAND_MAX is %d\n".

If plain char is signed, the conversion of i from int to char (which
occurs with or without the cast) can produce an implementation-defined
result or raise an implementation-defined signal. If you want to
produce numbers within a specified range, you should do the necessary
arithmetic yourself.

If you want random lowercase letters, you can declare

const char letters[] = "abcdefghijklmnopqrstuvwxyz"

and index into the array with a random number in the range 0..25.

Section 13 of the comp.lang.c FAQ, <http://www.c-faq.com/>, has
several questions about random numbers.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-11-2010
On 10 Jan, 23:05, frank <fr...@example.invalid> wrote:
> Nick Keighley wrote:
> > On 10 Jan, 07:35, frank <fr...@example.invalid> wrote:


> >> I intend to write a couple C utilities [...]. *
> >> Where I'm stuck right now is that I can't seem to find source
> >> for invoking pseudo-random behaviour. *So here's my first attempt:


<snip>

> > try the FAQhttp://c-faq.com/lib/index.html
> > particularly questions 13.15 and 13.16

>
> The FAQ wouldn't load but I have a hard copy here that gave me what
> seems to be an appropriate seed:


<snip>

> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(void)
> {
>
> int i;
> char c;
> srand((unsigned int)time((time_t *)NULL));
>
> i=rand();
> printf("i is %d\n", i);
> printf("i is %d\n", RAND_MAX);
> c=(char)i;


what do you think this does?

> printf("c is %c\n", c);
>
> return 0;}


<snip>

> What I want to do now is produce equiprobable chars.


see FAQ 13.6

>*If I wanted all
> the chars, then I think that the above would suffice,


and likely a lot of other stuff

> but I want only lower case letters.
>
> I could use j = rand()%26


that produces badly distributed numbers in the rnage 0..25. The lower
case letters are not in the range 0..25 in most character sets. Use
FAQ 13.6 to get yourself a uniform distribution of numbers in the
range in the range 0..25. The use Keiths idea or add 'a' (which will
work for ASCII).

> or some such and add that character to a, but
> I want to be mindful of 13.16, which warns against this. *Can someone
> say a few words about this?


do what 13.6 says?
 
Reply With Quote
 
Frank
Guest
Posts: n/a
 
      01-11-2010
On 1/11/2010 2:35 AM, Nick Keighley wrote:
> On 10 Jan, 23:05, frank<fr...@example.invalid> wrote:
>> Nick Keighley wrote:
>>> On 10 Jan, 07:35, frank<fr...@example.invalid> wrote:

>
>>>> I intend to write a couple C utilities [...].
>>>> Where I'm stuck right now is that I can't seem to find source
>>>> for invoking pseudo-random behaviour. So here's my first attempt:

>
> <snip>
>
>>> try the FAQhttp://c-faq.com/lib/index.html
>>> particularly questions 13.15 and 13.16

>>
>> The FAQ wouldn't load but I have a hard copy here that gave me what
>> seems to be an appropriate seed:

>
> <snip>
>
>> #include<stdio.h>
>> #include<stdlib.h>
>> #include<time.h>
>>
>> int main(void)
>> {
>>
>> int i;
>> char c;
>> srand((unsigned int)time((time_t *)NULL));
>>
>> i=rand();
>> printf("i is %d\n", i);
>> printf("i is %d\n", RAND_MAX);
>> c=(char)i;

>
> what do you think this does?


Wouldn't this be a demotion? Mapping onto a smaller set like modular
athimetic. It *should* be able to produce any char in the set, and
somewhat equiprobably.
>
>> printf("c is %c\n", c);
>>
>> return 0;}

>
> <snip>
>
>> What I want to do now is produce equiprobable chars.

>
> see FAQ 13.6


I think they've switched numbers on the on-line ones:

Q: How can I split up a string into whitespace-separated fields?
How can I duplicate the process by which main() is handed argc and argv?

>
>> If I wanted all
>> the chars, then I think that the above would suffice,

>
> and likely a lot of other stuff
>
>> but I want only lower case letters.
>>
>> I could use j = rand()%26

>
> that produces badly distributed numbers in the rnage 0..25. The lower
> case letters are not in the range 0..25 in most character sets. Use
> FAQ 13.6 to get yourself a uniform distribution of numbers in the
> range in the range 0..25. The use Keiths idea or add 'a' (which will
> work for ASCII).
>
>> or some such and add that character to a, but
>> I want to be mindful of 13.16, which warns against this. Can someone
>> say a few words about this?

>
> do what 13.6 says?


Thanks all for responses. I have to run right now but had source tyo
discuss.
--
frank
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-11-2010
Frank <> writes:
> On 1/11/2010 2:35 AM, Nick Keighley wrote:
>> On 10 Jan, 23:05, frank<fr...@example.invalid> wrote:

[...]
>>> int i;
>>> char c;

[...]
>>> c=(char)i;

>>
>> what do you think this does?

>
> Wouldn't this be a demotion? Mapping onto a smaller set like modular
> athimetic. It *should* be able to produce any char in the set, and
> somewhat equiprobably.

[...]

It's a conversion; since it converts from a wider type to a narrower
type, I suppose you could call it a demotion.

But what makes you think modular arithmetic is involved?

Plain char may be either signed or unsigned. If it's unsigned, then
yes, the result of the conversion is reduced modulo UCHAR_MAX+1
(that's modulo 256 on systems with 8-bit bytes). But if it's signed,
and the int value is outside the range CHAR_MIN..CHAR_MAX, then the
result of the conversion is implementation-defined -- *or* an
implementation-defined signal is raised.

I already mentioned this elsethread, though not in quite so much
detail.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Special Gift Gift Idea using Your Photo! emyface2@hotmail.com Digital Photography 0 04-03-2006 11:07 AM
OT: Gift? Kat MCSE 11 03-11-2005 09:23 PM
Fill out a short survey and win $100 gift certificate. jilp Cisco 0 11-24-2003 06:01 PM
OT gift ideas? Consultant MCSE 1 10-22-2003 12:36 AM
Re: A Gift for the MCSE that has everything Kendal Emery MCSE 1 08-01-2003 02:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57