rand() related ??

V

vib

Hi there,

i) Is there any other way to replace the rand()of standard C lib?
ii) How efficient is the rand()?

Thanks in advance.

vib
 
P

Paul Mesken

Hi there,

i) Is there any other way to replace the rand()of standard C lib?
ii) How efficient is the rand()?

Volume 2 of Donald Knuth's "The Art of Computer Programming",
"Seminumerical Algorithms" devotes an entire chapter to random numbers
and methods to test them (all in all almost 200 pages).

Here are some links :

http://random.mat.sbg.ac.at/links/index.html
http://www.randomnumbers.info/
http://www.fourmilab.ch/hotbits/

As you can see, you can even download some random numbers. You could
also use some file (preferably one that involves some compression.
 
E

Eric Sosman

vib said:
Hi there,

i) Is there any other way to replace the rand()of standard C lib?

There is no way to replace any part of the Standard
library -- rand(), printf(), exit(), whatever -- while
remaining within the scope of Standard C. A particular
C implementation may provide ways to do this, but if so
they are specific to the implementation and not defined
by the C language; search the implementation's documentation
or ask in a forum that discusses the implementation in
question.

However, it is perfectly all right to create your own
myrand() function that generates pseudo-random numbers using
whatever method you choose. Many methods exist, each with
its own strengths and weaknesses.
ii) How efficient is the rand()?

Each C implementation supplies its own version of rand(),
and the versions need not be identical. Thus, the question
can only be answered in terms of a particular implementation,
not in terms of the C language itself.

If you want an extremely efficient source of pseudo-random
numbers, feel free to use this one:

int myrand(void) { return 0; }

This is about the fastest you're likely to get, although the
statistical properties of the generated number sequence may
be inadequate for some applications ...

(In other words, your question cannot be important until
many other questions have been asked and answered.)
 
I

indiangeek

vib said:
Hi there,

i) Is there any other way to replace the rand()of standard C lib?
ii) How efficient is the rand()?

Thanks in advance.

vib

I don't think it's possible to replace something in the standard C lib.
However, if you have a lot of code written using rand() and now you want to
use myrand, you can #define rand to, say, myrand at the beginning of each
file (or in some header file that gets included in all of the files that
uses rand). Not sure if this applies to your specific case though!!
 
K

Keith Thompson

I don't think it's possible to replace something in the standard C lib.
However, if you have a lot of code written using rand() and now you want to
use myrand, you can #define rand to, say, myrand at the beginning of each
file (or in some header file that gets included in all of the files that
uses rand). Not sure if this applies to your specific case though!!

Yes, you can, but it's almost certainly a bad idea. Future
maintenance programmers will curse your name unless you actually
replace the rand() calls with explicit calls to myrand(). If I see
something that appears to be a call to rand(), I'm going to assume
that it's a call to the standard C function of that name, with
everything that implies.

The macro approach might be barely acceptable if your function
implements the semantics guaranteed by the standard function
(including the interaction with the srand() function and the RAND_MAX
macro). Even then, though, I'd rather use distinct names.
 

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
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top