473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

rand() related ??

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

Nov 14 '05 #1
5 1738
On 2 Jun 2005 06:43:20 -0700, "vib" <vi*****@gmail.com> wrote:
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.

Nov 14 '05 #2


vib wrote:
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.)

--
Er*********@sun.com

Nov 14 '05 #3
vib

Hi Paul and all

The links are really helpful. Thanks

Paul Mesken wrote:
On 2 Jun 2005 06:43:20 -0700, "vib" <vi*****@gmail.com> wrote:
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.


Nov 14 '05 #4
vib wrote:
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!!
--
-IG
Nov 14 '05 #5
in********@gmail.com writes:
vib wrote:
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!!


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Tom McCallum | last post by:
Can any tell me why rand() is such a bad pseudo-random number generator. In all the articles I have read they say that you can predict the outcome of rand() but when I used its output with NIST's...
7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
36
by: Profetas | last post by:
Hi, I want to generate a random 8 bit number using rand(0 is that possible? to expecifu the base and the lenght? thanks
8
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make...
4
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
13
by: Spiros Bousbouras | last post by:
The standard says that rand() should return a pseudo-random number but what does pseudorandom mean ? If an implementation of rand() always returned the same number would it be conforming ? What if...
5
by: ds | last post by:
Hi all, rand() is not thread safe, a fact that may not be so bad after all.. However, I face the following problem: a piece of code uses rand() to get a random sequence, but always seeds with...
10
by: Rafael Cunha de Almeida | last post by:
Hi, I've found several sites on google telling me that I shouldn't use rand() % range+1 and I should, instead, use something like: lowest+int(range*rand()/(RAND_MAX + 1.0))
15
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.