473,799 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1762
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",
"Seminumeri cal 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",
"Seminumeri cal 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********@gmai l.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_Keit h) 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
4225
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 random number testsuite it passed all of the tests thrown at it for randomness. Can anyone suggest a test I could use that it would fail? A pointer to a suitable article would be appreciated. Thanks for your help in advance, Tom
7
3249
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 library, which is based on GTK. Depending on which libraries I link in, I can have either a GTK1 or GTK2 user interface. I'm finding that the simulation behaves differently depending on whether I link with GTK1 or GTK2.
36
2696
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
4395
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 500,000 consecutive rand calls then is the rand() algorithm going to guarantee me that no floating window of calls over that 500,000 rand calls will have the same value twice? The only way that would work is if the series was identical everytime.
4
2798
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 my language simply propogates up to the rand( ) function from c++. For some reason, C++ will give each thread independent use of their own rand( ) function, so that a rand( ) call from one thread won't affect another thread's call to rand( )....
13
3671
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 it always alternated between 2 values ? On the practical side do you have any thoughts on what one could realistically expect from the behaviour of rand() ? Could for example one expect that eventually any value in the range will be returned ?
5
2325
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 the same seed for reproducibility of the results. Now I am porting this (old C89) code and have setup a nice app with threads that drives on one thread the old code and on another the new code, so that I can compare the results and see that nothing...
10
3022
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
3948
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 of two (or near a power of two), that this is not an issue. The upper bits will participate equally in the result with the lower. Am I missing something? Thanks!
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.