I just tried to write a small program based on it :
#include <stdio.h>
#include <stdlib.h>
#define PRECISION 2.82e14
double drand48(void)
{
double x = 0;
double denom = RAND_MAX + 1;
double need;
for(need = PRECISION; need 1; need /= (RAND_MAX + 1.))
{
x += rand()/denom;
denom *= RAND_MAX + 1. ;
}
return x;
}
int main(void)
{
double x;
x = drand48();
printf("%f" ,x);
x = drand48();
printf("%f", x);
return 0;
}
But each time I get the same output 0. What could be wrong here ?
Does it mean that this will give teh same values over and over again
everytime its called ? 3 3455
pereges wrote:
I just tried to write a small program based on it :
#include <stdio.h>
#include <stdlib.h>
#define PRECISION 2.82e14
double drand48(void)
{
double x = 0;
double denom = RAND_MAX + 1;
double need;
for(need = PRECISION; need 1; need /= (RAND_MAX + 1.))
{
x += rand()/denom;
denom *= RAND_MAX + 1. ;
}
return x;
}
int main(void)
{
double x;
x = drand48();
printf("%f" ,x);
x = drand48();
printf("%f", x);
return 0;
}
But each time I get the same output 0. What could be wrong here ?
Does it mean that this will give teh same values over and over again
everytime its called ?
Using lcc-win I obtain
0.001268 0.585006
Of course I inserted a space after the first number.
It seems to be working
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique http://www.cs.virginia.edu/~lcc-win32
pereges wrote:
I just tried to write a small program based on it :
#include <stdio.h>
#include <stdlib.h>
#define PRECISION 2.82e14
double drand48(void)
{
double x = 0;
double denom = RAND_MAX + 1;
Are you sure this is copied correctly? As written here,
this is likely to misbehave if RAND_MAX == INT_MAX; to fix
that, change the final `1' to `1.0'.
-- Er*********@sun .com
On Mon, 21 Apr 2008 05:33:54 -0700 (PDT), pereges <Br*****@gmail. com>
wrote:
>I just tried to write a small program based on it :
#include <stdio.h> #include <stdlib.h>
#define PRECISION 2.82e14
double drand48(void) { double x = 0; double denom = RAND_MAX + 1; double need;
for(need = PRECISION; need 1; need /= (RAND_MAX + 1.))
{ x += rand()/denom; denom *= RAND_MAX + 1. ;
}
return x;
}
int main(void) {
double x; x = drand48(); printf("%f" ,x); x = drand48(); printf("%f", x);
return 0;
}
But each time I get the same output 0. What could be wrong here ?
That's not the result I get. Did you cut and paste your code or
retype it?
>Does it mean that this will give teh same values over and over again everytime its called ?
Since your loop always executes the same number of times (four), the
return value is determined completely by the sequence of values
returned by rand. If you force rand to return the same sequence (such
as by calling srand with the same seed before each call to your
function), then you will get the same value each time. Otherwise not.
As it is now, since you don't call srand at all, your code will
produce the same sequence each time the program is run.
Remove del for email This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ZF Tang |
last post by:
Hi, guys.
I'm compiling a c++ file in cygwin.
drand48() and srand48() are used in this file. But there is no definition
for them in stdlib.h. Can I replace them with random()? If no, is there any
other approach to solve this problem?
I'm not familiar with Unix/Linux-like environments. Any help will be
appreciate very much.
|
by: Enrico `Trippo' Porreca |
last post by:
Both K&R book and Steve Summit's tutorial define a getline() function
correctly testing the return value of getchar() against EOF.
I know that getchar() returns EOF or the character value cast to
unsigned char.
Since char may be signed (and if so, the return value of getchar() would
be outside its range), doesn't the commented line in the following code
produce implementation-defined behaviour?
|
by: Steve W. |
last post by:
I just read the section (and did the exercise) in the C#
Step by Step book that covers Explict Interface
Implementation (where you specify in the method
implementation the specific interface that you are
implementing in the class.
Other than to resolve the problem that arises when a class
implements two interfaces with the same method signature,
what good is it?
|
by: arnuld |
last post by:
this is the programme i created, for exercise 2, assignment 3 at
http://www.eskimo.com/~scs/cclass/asgn.beg/PS2.html
it runs fine. i wanted to know if it needs any improvement:
----------------- PROGRAMME ----------------------------
/* Steve Summit's C programming
Section 3 :: exercise 2
|
by: arnuld |
last post by:
it is a very simple and it runs fine BUT still i want to have some
views on this:
-------------- PROGRAMME------------------------------
#include <stdio.h>
int main()
{
int i;
| |
by: arnuld |
last post by:
again, i am looking for some good advice
:-)
-------------- PROGRAMME ----------------
/* Steve Summit's C programming
assignment 3, exercise 5
STATEMENT:
|
by: user923005 |
last post by:
/*
** Direct computation of Fibonacci numbers.
**
** Input: index of Fibonacci number to compute (n)
**
** Returns: nth Fibonacci number.
*/
#include <math.h>
double fibonacci(unsigned n)
{
|
by: JosAH |
last post by:
Greetings,
Introduction
At this moment we have a TextProcessor, a LibraryBuilder as well as the Library
itself. As you read last week a Library is capable of producing pieces of text
in a simple way. We also briefly mentioned the BookMark which represents a
single paragraph of text. We haven't seen it's implementation yet. This is
the topic of this week's article part.
|
by: pereges |
last post by:
I came across a code that calculates N uniformly distributed points on
the sphere. z is a random number between -1 and 1 and to calculate it
we use drand48 function which returns a uniformly distributed value
between 0 and 1. What I don't understand is how it actually works ?
What does he actually mean by "uniformly distributed values" ?
void SpherePoints(int n, double X, double Y, double Z)
{
|
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...
|
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,...
| |
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |