473,666 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rule for promotion

Hi All,
I hope I am posting a C question instead of a C++ this time. I have
the following program:

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

int main(int argc,char** argv){
int i;
srand(time(0));
for(i = 0; i < 20; i++){
int x = 1 + (int) (20.0 * rand() / (RAND_MAX + 1.0));
int y = 1 + (int) (20 * rand() / (RAND_MAX + 1.0));
int z = 1 + (int) (20.0 * rand() / (RAND_MAX + 1 ));
printf("%d %d %d\n",x,y,z);
}
}

and it prints:

12 1 -18
4 1 -17
14 1 -12
17 1 -13
8 1 0
9 1 -2
.... etc

it looks like x is always between 1 and 20. y is always 1 and z is
always between negative and 0. i don't understand why. i guess i
understand why x is always positive because the whole expression is
evaluated in long(?) so there won't be any overflow, right? But is y
and z behave so differently? I though C promote the expression if one
of them is long instead of int, right? Can someone explain what
happened? Thanks!
Nov 14 '05 #1
6 1505

"pembed2003 " <pe********@yah oo.com> wrote in message

int z = 1 + (int) (20.0 * rand() / (RAND_MAX + 1 ));
it looks like x is always between 1 and 20. y is always 1 and z is
always between negative and 0. i don't understand why.

The expression rand() / (RAND_MAX + 1) will be evaluated first as an integer
expression. RAND_MAX on your machine must equal INT_MAX, so RAND_MAX + 1 is
the lowest possible negative integer.
Nov 14 '05 #2
In article <c5**********@n ewsg1.svr.pol.c o.uk>,
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:
"pembed2003 " <pe********@yah oo.com> wrote in message

int z = 1 + (int) (20.0 * rand() / (RAND_MAX + 1 ));
it looks like x is always between 1 and 20. y is always 1 and z is
always between negative and 0. i don't understand why.

The expression rand() / (RAND_MAX + 1) will be evaluated first as an integer
expression. RAND_MAX on your machine must equal INT_MAX, so RAND_MAX + 1 is
the lowest possible negative integer.


You should really know that this is not true.

20.0 * rand() / (RAND_MAX + 1) is evaluated as follows:

20.0 => Value 20, type double.
rand () => Random value, type int, from 0 to RAND_MAX
(RAND_MAX + 1) => Type int. If RAND_MAX == INT_MAX then -> overflow,
undefined behavior; many implementations will produce INT_MIN.

20.0 * rand () => rand () is converted to double, result is of type
double, from 0 to 20 * RAND_MAX.
20.0 * rand () / (RAND_MAX + 1) => RAND_MAX + 1 is converted to
double. If there was no overflow in calculating RAND_MAX + 1 then the
result is of type double, >= 0.0 and < 20.0. If there is an overflow,
then undefined behavior, but most likely the result of (RAND_MAX + 1) is
INT_MIN, which is often - (INT_MAX + 1) = - (RAND_MAX + 1), so the
result is <= 0 and > -20.
Nov 14 '05 #3
Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote in message news:<ch******* *************** ***********@slb-newsm1.svr.pol. co.uk>...
In article <c5**********@n ewsg1.svr.pol.c o.uk>,
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:
"pembed2003 " <pe********@yah oo.com> wrote in message

int z = 1 + (int) (20.0 * rand() / (RAND_MAX + 1 ));
it looks like x is always between 1 and 20. y is always 1 and z is
always between negative and 0. i don't understand why.

The expression rand() / (RAND_MAX + 1) will be evaluated first as an integer
expression. RAND_MAX on your machine must equal INT_MAX, so RAND_MAX + 1 is
the lowest possible negative integer.


You should really know that this is not true.

20.0 * rand() / (RAND_MAX + 1) is evaluated as follows:

20.0 => Value 20, type double.
rand () => Random value, type int, from 0 to RAND_MAX
(RAND_MAX + 1) => Type int. If RAND_MAX == INT_MAX then -> overflow,
undefined behavior; many implementations will produce INT_MIN.

20.0 * rand () => rand () is converted to double, result is of type
double, from 0 to 20 * RAND_MAX.
20.0 * rand () / (RAND_MAX + 1) => RAND_MAX + 1 is converted to
double. If there was no overflow in calculating RAND_MAX + 1 then the
result is of type double, >= 0.0 and < 20.0. If there is an overflow,
then undefined behavior, but most likely the result of (RAND_MAX + 1) is
INT_MIN, which is often - (INT_MAX + 1) = - (RAND_MAX + 1), so the
result is <= 0 and > -20.


Thanks for the explaination. Here is how I look at it:

(20.0 * rand() / (RAND_MAX + 1 ))

If I put parenthesis:

(((20.0) * (rand())) / ((RAND_MAX) + (1)))

so essentially:

(((double) * (int)) / ((int) + (int)))

double * int gives:

((double) / ((int) + (int)))

now since the second expression overflow (in my machine RAND_MAX ==
MAX_INT), we have:

((doubld) / ((MAX_INT) + (1))) = ((double) / (overflow))

which results in a nagetive number. make sense. But I can't understand
why:

(20 * rand() / (RAND_MAX + 1.0))

always result in a 1?

(((20) * (rand())) / ((RAND_MAX) + (1.0)))

so it's:

(((int) * (int)) / ((int) + (double)))

and then:

(((int) * (int)) / (double))

and then from here, I can't understand why it's always a 1. I can
understand that an int * int might overflow but will C promote it to a
double because we have a double just on the right side of /

Thanks!
Nov 14 '05 #4

On Fri, 15 Apr 2004, pembed2003 wrote:

Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote

20.0 * rand() / (RAND_MAX + 1) is evaluated as follows:

20.0 => Value 20, type double.
rand () => Random value, type int, from 0 to RAND_MAX
(RAND_MAX + 1) => Type int. If RAND_MAX == INT_MAX then -> overflow,
undefined behavior; many implementations will produce INT_MIN.

20.0 * rand () => rand () is converted to double, result is of type
double, from 0 to 20 * RAND_MAX.
20.0 * rand () / (RAND_MAX + 1) => RAND_MAX + 1 is converted to
double. If there was no overflow in calculating RAND_MAX + 1 then the
result is of type double, >= 0.0 and < 20.0. If there is an overflow,
then undefined behavior, but most likely the result of (RAND_MAX + 1) is
INT_MIN, which is often - (INT_MAX + 1) = - (RAND_MAX + 1), so the
result is <= 0 and > -20.

[Why does]
(20 * rand() / (RAND_MAX + 1.0))

always result in a 1?
It doesn't, of course. It results in a floating-point 'double'
value, on your machine probably between -1.0 and 1.0 (even though of
course technically it could do whatever it liked).
Consider:

20 --> type 'int', value 20
rand() --> type 'int', value 0..RAND_MAX
20 * rand() --> type 'int', value 0..20*RAND_MAX

Now, at this point we realize that RAND_MAX is probably equal to
INT_MAX (as it is on most modern systems); so 20*RAND_MAX overflows
and we have a case of undefined behavior. Let's assume that 'int'
values overflow by "wrapping around," so that the value of 20*rand()
is still a random number between INT_MIN and INT_MAX.

20 * rand() --> type 'int', value INT_MIN..INT_MA X
RAND_MAX + 1.0 --> type 'double', value RAND_MAX+1
whole expression --> type 'double', value...

The value of the whole expression, assuming what we assumed above about
overflow on your machine, is a random number between
(INT_MIN/(RAND_MAX+1.)) and (INT_MAX/(RAND_MAX+1.)) . And since RAND_MAX
is assumed to be INT_MAX, and we can also assume that on your machine
INT_MIN is just -INT_MAX-1, our resulting value is a 'double' value
between -1.0 and INT_MAX/(INT_MAX+1).
Thanks!


You're welcome.

-Arthur

Nov 14 '05 #5
Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote in message news:<ch******* *************** ***********@slb-newsm1.svr.pol. co.uk>...
In article <c5**********@n ewsg1.svr.pol.c o.uk>,
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:
"pembed2003 " <pe********@yah oo.com> wrote in message

int z = 1 + (int) (20.0 * rand() / (RAND_MAX + 1 ));
it looks like x is always between 1 and 20. y is always 1 and z is
always between negative and 0. i don't understand why.

The expression rand() / (RAND_MAX + 1) will be evaluated first as an integer
expression. RAND_MAX on your machine must equal INT_MAX, so RAND_MAX + 1 is
the lowest possible negative integer.


You should really know that this is not true.

20.0 * rand() / (RAND_MAX + 1) is evaluated as follows:

20.0 => Value 20, type double.
rand () => Random value, type int, from 0 to RAND_MAX
(RAND_MAX + 1) => Type int. If RAND_MAX == INT_MAX then -> overflow,
undefined behavior; many implementations will produce INT_MIN.

20.0 * rand () => rand () is converted to double, result is of type
double, from 0 to 20 * RAND_MAX.
20.0 * rand () / (RAND_MAX + 1) => RAND_MAX + 1 is converted to
double. If there was no overflow in calculating RAND_MAX + 1 then the
result is of type double, >= 0.0 and < 20.0. If there is an overflow,
then undefined behavior, but most likely the result of (RAND_MAX + 1) is
INT_MIN, which is often - (INT_MAX + 1) = - (RAND_MAX + 1), so the
result is <= 0 and > -20.


Thanks for the explaination. Here is how I look at it:

(20.0 * rand() / (RAND_MAX + 1 ))

If I put parenthesis:

(((20.0) * (rand())) / ((RAND_MAX) + (1)))

so essentially:

(((double) * (int)) / ((int) + (int)))

double * int gives:

((double) / ((int) + (int)))

now since the second expression overflow (in my machine RAND_MAX ==
MAX_INT), we have:

((doubld) / ((MAX_INT) + (1))) = ((double) / (overflow))

which results in a nagetive number. make sense. But I can't understand
why:

(20 * rand() / (RAND_MAX + 1.0))

always result in a 1?

(((20) * (rand())) / ((RAND_MAX) + (1.0)))

so it's:

(((int) * (int)) / ((int) + (double)))

and then:

(((int) * (int)) / (double))

and then from here, I can't understand why it's always a 1. I can
understand that an int * int might overflow but will C promote it to a
double because we have a double just on the right side of /

Thanks!
Nov 14 '05 #6

On Fri, 15 Apr 2004, pembed2003 wrote:

Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote

20.0 * rand() / (RAND_MAX + 1) is evaluated as follows:

20.0 => Value 20, type double.
rand () => Random value, type int, from 0 to RAND_MAX
(RAND_MAX + 1) => Type int. If RAND_MAX == INT_MAX then -> overflow,
undefined behavior; many implementations will produce INT_MIN.

20.0 * rand () => rand () is converted to double, result is of type
double, from 0 to 20 * RAND_MAX.
20.0 * rand () / (RAND_MAX + 1) => RAND_MAX + 1 is converted to
double. If there was no overflow in calculating RAND_MAX + 1 then the
result is of type double, >= 0.0 and < 20.0. If there is an overflow,
then undefined behavior, but most likely the result of (RAND_MAX + 1) is
INT_MIN, which is often - (INT_MAX + 1) = - (RAND_MAX + 1), so the
result is <= 0 and > -20.

[Why does]
(20 * rand() / (RAND_MAX + 1.0))

always result in a 1?
It doesn't, of course. It results in a floating-point 'double'
value, on your machine probably between -1.0 and 1.0 (even though of
course technically it could do whatever it liked).
Consider:

20 --> type 'int', value 20
rand() --> type 'int', value 0..RAND_MAX
20 * rand() --> type 'int', value 0..20*RAND_MAX

Now, at this point we realize that RAND_MAX is probably equal to
INT_MAX (as it is on most modern systems); so 20*RAND_MAX overflows
and we have a case of undefined behavior. Let's assume that 'int'
values overflow by "wrapping around," so that the value of 20*rand()
is still a random number between INT_MIN and INT_MAX.

20 * rand() --> type 'int', value INT_MIN..INT_MA X
RAND_MAX + 1.0 --> type 'double', value RAND_MAX+1
whole expression --> type 'double', value...

The value of the whole expression, assuming what we assumed above about
overflow on your machine, is a random number between
(INT_MIN/(RAND_MAX+1.)) and (INT_MAX/(RAND_MAX+1.)) . And since RAND_MAX
is assumed to be INT_MAX, and we can also assume that on your machine
INT_MIN is just -INT_MAX-1, our resulting value is a 'double' value
between -1.0 and INT_MAX/(INT_MAX+1).
Thanks!


You're welcome.

-Arthur

Nov 14 '05 #7

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

Similar topics

10
2262
by: Niels Dekker (no reply address) | last post by:
Is it possible for a standard compliant C++ compiler to have ( sizeof(short) < sizeof(int) ) and ( sizeof(short) == sizeof((short)0 + (short)0) ) ? Regards, Niels Dekker http://www.xs4all.nl/~nd/dekkerware
8
4841
by: BigMan | last post by:
Can someone cite the rules for type promotion in C++? And, in particular, what is the type of the result of adding 2 values of type char?
2
2158
by: Andy | last post by:
Hi... i'm trying to understand the concept of function name overloading in c++. to understand the resolving system it's important to understand the diffrent levels of typecasting (exact match, promotion, conversion, not possible) that is, what i don't really understand. so i tried do make a little
112
4355
by: Carsten Hansen | last post by:
Suppose I'm using an implementation where an int is 16 bits. In the program below, what function is called in the first case, and what is called in the second case? Also, if there is a difference between C89 and C99, I would like to know. I have tried with different compilers, and I see some differences. Before I file a bug report with my C vendor, I would like to know what the correct behavior is. struct S
16
5118
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
4
1140
by: pachanga | last post by:
Last week, I got a promotion to a higher position with new responsibilities and new supervisor. My supervisor ask me about a new position opening, and I took the position, but I totally forgot to ask how much is my raise. A week has past and no one has mention it to me. Next week I start training. What should I do? I feel kinda weird, one thing is I do want to know now. And, the other is should I wait till I go to the new position? What is...
8
1321
by: Jon Paul Jones | last post by:
For some time now, I have been looking for a build system for ASP.NET that will merge the file based ease of deployment of classic ASP with the type saftey and prebuilt nature of ASP.NET with codebehind. The team that I work on manages several web sites and we have many different projects (not VS projects) going on in each site simultaneously. Each of these projects may have different deployment schedules. In a classis ASP world this was...
4
1679
by: gen_tricomi | last post by:
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet....
21
4107
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me. Could someone please explain to me what's going on? I would have thought that the following happens: (1) The literal, 0, whose type is int, gets converted to an unsigned char.
6
13816
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
0
8444
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
8356
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
8781
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
8551
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,...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
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.