Can you tell me what is wrong with this? 
Author Message
 Can you tell me what is wrong with this?

Can you tell me what is wrong with this random function?

Zach

[ hurray.c < 1K ]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ELEMENTS 20

int rnd(int range);
void seedrnd(void);

void main()
{
int blorf[ELEMENTS];
int x;

printf("\nAnd now random numbers:\n");

for(x=0;x<ELEMENTS;x++)
{
blorf[x] = rnd(6)+1;
printf("%i\x6",blorf[x]);

Quote:
}
}

int rnd(int range)
{
int r;
r=rand()%range;
return(r);
Quote:
}

void seedrnd(void)
{
srand((unsigned)time(NULL));
Quote:
}



Sat, 21 Apr 2001 03:00:00 GMT  
 Can you tell me what is wrong with this?

Quote:

> Can you tell me what is wrong with this random function?

> Zach

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

> #define ELEMENTS 20

> int rnd(int range);
> void seedrnd(void);

> void main()
> {
> int blorf[ELEMENTS];
> int x;

> printf("\nAnd now random numbers:\n");

> for(x=0;x<ELEMENTS;x++)
> {
> blorf[x] = rnd(6)+1;
> printf("%i\x6",blorf[x]);

I cannot see anything wrong about it. Except for the strange printf
format.

It should generate a sequence rand # between 1 and 6.



Sat, 21 Apr 2001 03:00:00 GMT  
 Can you tell me what is wrong with this?

Quote:

> Can you tell me what is wrong with this random function?

Hi Zach Gorman,

Well, what is wrong with it from your point of view ? You might
have explained a bit what you expect to get and what you get instead.

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

> #define ELEMENTS 20

> int rnd(int range);
> void seedrnd(void);

> void main()

This is not correct ANSI-C. The only correct return type for "main"
is "int". So this should be:
   int main( void )

Quote:
> {
> int blorf[ELEMENTS];
> int x;

> printf("\nAnd now random numbers:\n");

Hmmm, somewhere around this point (ie. before the first call to
"rnd()") I would have expected you to call "seedrnd()". Since you
do not do this, you are going to get the same sequence of random
numbers each time the program is called.

Quote:
> for(x=0;x<ELEMENTS;x++)
> {
> blorf[x] = rnd(6)+1;
> printf("%i\x6",blorf[x]);

Hmmm, I wonder what "\x6" is ment to achieve (in the ASCII chart it's
ACK). But what I am missing is a '\n'. There should be at least one
before the program terminates.

Quote:
> int rnd(int range)
> {
> int r;
> r=rand()%range;

This is another problematic thing. It might not be the thing troubling
you, but "%" typically gives poor random numbers. This effect and
a really good solution is explained in the comp.lang.c FAQ list:
   13.16:  How can I get random integers in a certain range?

You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
posted to this newsgroup and to news.answers regularly (at the
beginning of each month).

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 22 Apr 2001 03:00:00 GMT  
 Can you tell me what is wrong with this?
---------------------------------------------------------------
: #include <stdio.h>
: #include <stdlib.h>
: #include <time.h>
:
: #define ELEMENTS 20
:
: int rnd(int range);
: void seedrnd(void);
:
: void main()
int main(void) to comply with the standard
: {
: int blorf[ELEMENTS];
: int x;
:
: printf("\nAnd now random numbers:\n");
:
add a seedrnd(); here so the seed is set...
: for(x=0;x<ELEMENTS;x++)
: {
: blorf[x] = rnd(6)+1;
: printf("%i\x6",blorf[x]);
: }
:
: }
:
: int rnd(int range)
: {
: int r;
: r=rand()%range;
: return(r);
: }
: void seedrnd(void)
: {
: srand((unsigned)time(NULL));
: }
you might have made it easier to people diagnose the problem
if you specify the trouble your having with it?
does it keep giving you the same numbers over and over again
or is the number returned not in the range you want it
or does it not even compile?

Ed



Sun, 22 Apr 2001 03:00:00 GMT  
 Can you tell me what is wrong with this?
What i meant is that it will not compile, try it, and you will see what I
mean.

Zach

Quote:


> > Can you tell me what is wrong with this random function?

> Hi Zach Gorman,

> Well, what is wrong with it from your point of view ? You might
> have explained a bit what you expect to get and what you get instead.

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

> > #define ELEMENTS 20

> > int rnd(int range);
> > void seedrnd(void);

> > void main()

> This is not correct ANSI-C. The only correct return type for "main"
> is "int". So this should be:
>    int main( void )

> > {
> > int blorf[ELEMENTS];
> > int x;

> > printf("\nAnd now random numbers:\n");

> Hmmm, somewhere around this point (ie. before the first call to
> "rnd()") I would have expected you to call "seedrnd()". Since you
> do not do this, you are going to get the same sequence of random
> numbers each time the program is called.

> > for(x=0;x<ELEMENTS;x++)
> > {
> > blorf[x] = rnd(6)+1;
> > printf("%i\x6",blorf[x]);

> Hmmm, I wonder what "\x6" is ment to achieve (in the ASCII chart it's
> ACK). But what I am missing is a '\n'. There should be at least one
> before the program terminates.

> > int rnd(int range)
> > {
> > int r;
> > r=rand()%range;

> This is another problematic thing. It might not be the thing troubling
> you, but "%" typically gives poor random numbers. This effect and
> a really good solution is explained in the comp.lang.c FAQ list:
>    13.16:  How can I get random integers in a certain range?

> You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
> at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
> posted to this newsgroup and to news.answers regularly (at the
> beginning of each month).

> Stephan
> (initiator of the campaign against grumpiness in c.l.c)



Sun, 22 Apr 2001 03:00:00 GMT  
 Can you tell me what is wrong with this?
Hi Zach,

Quote:
> What i meant is that it will not compile, try it, and you will see what I
> mean.

Ok, we are dealing with two separate problems here. On the one hand your
code has conceptual problems, like the missing call to "seedrnd()" and
on
the other hand it does not seem to compile (on your compiler). But first
I'd like to recommend to you to be a bit more verbose. Your postings are
a bit short on the words and explanations. Talkto me ! Talk to the
others !
Mention everything that is related to the problem :-)

For instance in the posting you did not mention that the code does not
compile (on your compiler). That's why I did not try to compile it.
But when looking through the code I did at least not spot any problems.
And guess what (you are maybe fearing this alread) ? Your code compiles
without error. I am refering to the code that you have posted in the
original message to this thread. For one thing I tested it on two
different ANSI-C compilers and for another there are no really big
bugs to be seen, besides those things I mentioned in my previous reply.

Please try again. Please give a thorough report of what compiler you
use and what messages it reports. I fear that we are dealing with
specific (mis)behaviour of your compiler here.

Oh and BTW: Please do not reply both by e-mail and by posting. This is
really more a nuisance than any help. I prefer posted replies and I can
live with e-mail replies, but both is quite confusing.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Mon, 23 Apr 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Please look at this code n tell me where i am wrong

2. Am I wrong or the compiler is wrong?

3. Am I wrong or compiler is wrong?

4. can somebody tell what is wrong?

5. Please tell me what went wrong...

6. can you tell me what's wrong with the code

7. Who can tell me where is wrong?

8. Can anyone tell me what is wrong?

9. Can anyone tell me what is wrong?

10. Please tell me what im doing wrong :(

11. Can anyone tell me what is wrong?

12. Can anyone tell me what is wrong?

 

 
Powered by phpBB® Forum Software