Random Number Generators....

K

Keith Thompson

Keith said:
srand((unsigned int)time((time_t *)NULL));
[snip]

Both casts are unnecessary. As long as the prototypes for srand() and
time() are visible (which they are since you have the proper #include
directives), the conversions will be done implicitly. Just use:

srand(time(NULL));

(You do sometimes need an explicit cast when you're calling a function
with a variable number of parameters, such as printf.)

Ok, thanks for pointing that out.

BTW, I copied it from the FAQ (some of us _do_ actually look
at the FAQ first).

I've just sent a note to Steve Summit.
 
R

RadiationX

Keith said:
RadiationX said:
could you give me an example of some sample code ?


/*
randtest.c
*/

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

int main()
{
float r[500][2];
int i;

srand((unsigned int)time((time_t *)NULL));
[snip]

Both casts are unnecessary. As long as the prototypes for srand() and
time() are visible (which they are since you have the proper #include
directives), the conversions will be done implicitly. Just use:

srand(time(NULL));

(You do sometimes need an explicit cast when you're calling a function
with a variable number of parameters, such as printf.)



Here is the code that I've written thus far


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

int randG();

int main()
{
int numDarts;
int hits=0;
int k;

srand(time(NULL));

printf("Enter The number of darts");
scanf("%d", numDarts);

for(k=0;k<=numDarts;++k);
{
randG();
}


system("pause");
return 0;
}

randG()

{
double x;
double y;

x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;

if((sqrt(x*x + y*y)<=1.0))

return 1;

else

return 0;

}
 
P

Pedro Graca

RadiationX said:
Here is the code that I've written thus far

Your code gave me a lot of warnings when I tried to compile it.
I suggest you turn up your compiler warning level and deal with warnings
as if they were errors (and therefore the code doesn't compile)

<OT>
for my compiler (gcc) I do

"gcc -W -Wall -Werror -std=c89 -pedantic source.c"
#include <stdio.h>
#include<math.h>
#include<stdlib.h>

#include said:
int randG();

int main()
{
int numDarts;
int hits=0;
int k;

srand(time(NULL));

printf("Enter The number of darts");

fflush(stdout); /* make sure printf output is done before
reaching the scanf() call */
scanf("%d", numDarts);

scanf("%d", &numDarts);
^
for(k=0;k<=numDarts;++k);
{
randG();
}

No matter what the value of numDarts is, randG() will execute exactly
once.
I think you don't want the last semicolon on the for() line.

Also randG() by itself will execute the function, return 0 or 1, and
promptly ignore that 0 or 1. You may want to do something with the
return value of randG() (maybe adding it to `hits').

Are you going to print something to let you know the number of hits your
program achieved?
system("pause");
return 0;
}

randG()

int randG()
[snip randG() implementation]
 
S

stathis gotsis

RadiationX said:
Here is the code that I've written thus far


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

Don't you need to include said:
int randG();

int main()
{
int numDarts;
int hits=0;
int k;

srand(time(NULL));

printf("Enter The number of darts");
scanf("%d", numDarts);

You mean: scanf("%d", &numDarts);
Maybe you should check for invalid user input later on and handle it
appropriately.
for(k=0;k<=numDarts;++k);

That semicolon is very harmful.
You are throwing numDarts+1 here, that will not make any difference,
remember that later though.
{
randG();

More stuff will be here in the future, maybe:

if (randG()) hits++;
}


system("pause");

That is a pretty system specific.
return 0;
}

randG()

Why not: int randG()?
{
double x;
double y;

x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;

if((sqrt(x*x + y*y)<=1.0))

That sqrt() is not absolutely needed there, if you choose to discard it you
 
M

mensanator

stathis said:
You mean: scanf("%d", &numDarts);
Maybe you should check for invalid user input later on and handle it
appropriately.


That semicolon is very harmful.
You are throwing numDarts+1 here, that will not make any difference,
remember that later though.


More stuff will be here in the future, maybe:

if (randG()) hits++;


That is a pretty system specific.


Why not: int randG()?


That sqrt() is not absolutely needed there, if you choose to discard it you
can get rid of <math.h> too.
How?
 
C

Charles Krug

Or tea. Or even ATS; it is the only known purpose for which that
substance is useful.

Or a device that extracts the whole of the Universe from a piece of
fairycake and then tells you "You are HERE"...
 
R

RadiationX

stathis said:
You mean: scanf("%d", &numDarts);
Maybe you should check for invalid user input later on and handle it
appropriately.


That semicolon is very harmful.
You are throwing numDarts+1 here, that will not make any difference,
remember that later though.


More stuff will be here in the future, maybe:

if (randG()) hits++;


That is a pretty system specific.


Why not: int randG()?


That sqrt() is not absolutely needed there, if you choose to discard it you


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

int randG();

int main()
{
int numDarts;
int hits=0;
int k;
double pi;

srand(time(NULL));

printf("Enter The number of darts");
scanf("%d", &numDarts);

for(k=0;k<=numDarts;++k);
{
hits = randG();
printf("%d\n",hits);
}





system("pause");
return 0;
}

randG()

{
double x;
double y;

x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;

if((sqrt(x*x + y*y)<=1.0))

return 1;

else

return 0;

}
 
S

stathis gotsis

RadiationX said:
>Here is my updated code. I'm not trying to get any quck answers, I really
want to learn this stuff. There are some things that I just don't have
enough experiece with, and this is one of them
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>

int randG();

int main()
{
int numDarts;
int hits=0;
int k;
double pi;

srand(time(NULL));

printf("Enter The number of darts");
scanf("%d", &numDarts);

for(k=0;k<=numDarts;++k);
{
hits = randG();

If you do it this way, hits will be set to 0 or 1. You need to count the
number of hits, maybe this way:

if (randG()) hits++;
printf("%d\n",hits);

That prints 0 or 1. It would be a nicer test if you printed the number of
hits after the above loop has ended.
}





system("pause");
return 0;
}

randG()

{
double x;
double y;

x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;

if((sqrt(x*x + y*y)<=1.0))

return 1;

else

return 0;

}

Maybe you should consider the comments made in previous posts as well.
 
M

mensanator

Richard said:
If a non-negative real number is smaller than or equal to 1.0, how large
must the square of that number be?

Oh, I get it.

If the x coordinate is less than 1, its square is less than 1.

And if the y coordinate is less than 1, its square is less than 1.

So if the squares of both are less than 1, then the distance
from the origin is less than 1.
1.27279220614

Uh, what went wrong?
 
J

Jordan Abel

Oh, I get it.


If the x coordinate is less than 1, its square is less than 1.
True.


And if the y coordinate is less than 1, its square is less than 1.
True.

So if the squares of both are less than 1, then the distance
from the origin is less than 1.
False.

1.27279220614

Uh, what went wrong?

The distance from the origin is 1.27. Hint: the distance from the origin
being less than one is determined by being inside the unit _circle_, not
the unit square.
 
M

mensanator

Jordan said:
The distance from the origin is 1.27. Hint: the distance from the origin
being less than one is determined by being inside the unit _circle_, not
the unit square.

Yeah, _I_ know that. I'm waiting to hear Richards Bos' explanation
of how to determine distance without using sqrt. The stuff about
how big the square of a number <1 must be doesn't cut it.
 
W

Walter Roberson

[/QUOTE]
Yeah, _I_ know that. I'm waiting to hear Richards Bos' explanation
of how to determine distance without using sqrt. The stuff about
how big the square of a number <1 must be doesn't cut it.

He didn't say that you could determine distances without using sqrt().
He said "that sqrt() is not absolutely needed there".

If sqrt(x*x+y*y) < c then by squaring both sides,
sqrt(x*x+y*y)*sqrt(x*x+y*y) < c*c so
x*x+y*y < c*c

but when c is 1.0 then c*c is also 1.0 so the test in the code
in question could be:

if ((x*x + y*y) <= 1.0)

Thus the sqrt() was indeed not absolutely needed there and could be
discarded in that particular code. Richard made no claim about other code.
 
P

Pedro Graca


sqrt(FOOBAR) <= 1.0

but 1.0 == sqrt(1.0), so that can written that as

sqrt(FOOBAR) <= sqrt(1.0)

and, as all the numbers are positive, the "sqrt()" can be safely removed

FOOBAR <= 1.0

to get an equivalent condition.
 
J

Jordan Abel

Yeah, _I_ know that. I'm waiting to hear Richards Bos' explanation
of how to determine distance without using sqrt. The stuff about
how big the square of a number <1 must be doesn't cut it.

But you're not looking at the square of X and the square of Y - you're
looking at the square of the distance, which is the sum of those two
squares.
 
M

mensanator

Yeah, _I_ know that. I'm waiting to hear Richards Bos' explanation
of how to determine distance without using sqrt. The stuff about
how big the square of a number <1 must be doesn't cut it.

He didn't say that you could determine distances without using sqrt().
He said "that sqrt() is not absolutely needed there".

If sqrt(x*x+y*y) < c then by squaring both sides,
sqrt(x*x+y*y)*sqrt(x*x+y*y) < c*c so
x*x+y*y < c*c

but when c is 1.0 then c*c is also 1.0 so the test in the code
in question could be:

if ((x*x + y*y) <= 1.0)

Thus the sqrt() was indeed not absolutely needed there and could be
discarded in that particular code. Richard made no claim about other code.[/QUOTE]

Ok, I see it.

Another fine example of the kind of thinking that makes space shuttles
explode.
 
K

Keith Thompson

Walter Roberson wrote: [...]
He didn't say that you could determine distances without using sqrt().
He said "that sqrt() is not absolutely needed there".

If sqrt(x*x+y*y) < c then by squaring both sides,
sqrt(x*x+y*y)*sqrt(x*x+y*y) < c*c so
x*x+y*y < c*c

but when c is 1.0 then c*c is also 1.0 so the test in the code
in question could be:

if ((x*x + y*y) <= 1.0)

Thus the sqrt() was indeed not absolutely needed there and could be
discarded in that particular code. Richard made no claim about other code.

Ok, I see it.

Another fine example of the kind of thinking that makes space shuttles
explode.

Huh???

The test
if (x*x + y*y <= 1.0)
is mathematically and practically equivalent to
if (sqrt(x*x + y*y) <= 1.0)
and likely to be substantially more efficient. We don't need to
compute the distance; we only care whether the distance is less than
or equal to 1.0, and we can easily determine that from the square of
the distance, which is easier to compute.

Are you implying that there's something dangerous about this simple
optimization? If so, what?
 
M

mensanator

Keith said:
Walter Roberson wrote: [...]
He didn't say that you could determine distances without using sqrt().
He said "that sqrt() is not absolutely needed there".

If sqrt(x*x+y*y) < c then by squaring both sides,
sqrt(x*x+y*y)*sqrt(x*x+y*y) < c*c so
x*x+y*y < c*c

but when c is 1.0 then c*c is also 1.0 so the test in the code
in question could be:

if ((x*x + y*y) <= 1.0)

Thus the sqrt() was indeed not absolutely needed there and could be
discarded in that particular code. Richard made no claim about other code.

Ok, I see it.

Another fine example of the kind of thinking that makes space shuttles
explode.

Huh???

The test
if (x*x + y*y <= 1.0)
is mathematically and practically equivalent to
if (sqrt(x*x + y*y) <= 1.0)
and likely to be substantially more efficient. We don't need to
compute the distance; we only care whether the distance is less than
or equal to 1.0, and we can easily determine that from the square of
the distance, which is easier to compute.

Are you implying that there's something dangerous about this simple
optimization? If so, what?

Never mind. I've re-thought it.
 
R

Richard Bos

Yeah, _I_ know that. I'm waiting to hear Richards Bos' explanation
of how to determine distance without using sqrt.
[/QUOTE]

You're not determining the exact distance, you're only trying to
discover whether it's smaller than a certain number. And since the
square root function is strictly ascending...

Do the actual math on the actual functions, not on an incoherent jumble
of words.
He didn't say that you could determine distances without using sqrt().
He said "that sqrt() is not absolutely needed there".

Actually, I didn't. stathis gotsis wrote that. All I did was agree, and
point out why.

Richard
 
M

mensanator

Richard said:
You're not determining the exact distance, you're only trying to
discover whether it's smaller than a certain number.

Yeah, it's already been cleared up.
And since the
square root function is strictly ascending...


Do the actual math on the actual functions, not on an incoherent jumble
of words.

I DID do the actual math. But because you were not clear,
I went down the wrong path and got nonsensical results.
And since I believed that I understood what I think you said,
it did not occur to me that what I heard was not what you
meant.
Actually, I didn't. stathis gotsis wrote that. All I did was agree, and
point out why.

The issue is settled as far as I'm concerned.
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top