473,467 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

cleaned up nrand48 function please check results ;)

Hi,

This is a somewhat cleaned up version 0.02 of the nrand48() routine.

There are still a few issues:

This code needs brackets but where ?

X = (uint64) xsubi[2] << 32 | (uint32) xsubi[1] << 16 | xsubi[0];

This code also needs brackets but where ?

*result = xsubi[2] << 15 | xsubi[1] >> 1;

Since operator precedence is different in other languages etc... so this
code needs brackets to be able to port/convert/translate it to any other
language, to prevent errors/bugs and for clearity sakes ;)

The last thing to check is the output.

Can somebody on a unix system... run this program... preferablly *without*
all the extra code... and use the original nrand48() function... and then
compare the unix output with this windows xp output to see if it's exactly
the same ? That would be great =D

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

typedef signed __int8 sint8;
typedef unsigned __int8 uint8;
typedef signed __int16 sint16;
typedef unsigned __int16 uint16;
typedef signed __int32 sint32;
typedef unsigned __int32 uint32;
typedef signed __int64 sint64;
typedef unsigned __int64 uint64;

/* Data structure for communication with thread safe versions. This
type is to be regarded as opaque. It's only exported because users
have to allocate objects of this type. */
struct drand48_data
{
uint16 __x[3]; /* Current state. */
uint16 __old_x[3]; /* Old state. */
uint16 __c; /* Additive const. in congruential formula. */
uint16 __init; /* Flag for initializing. */
uint64 __a; /* Factor in congruential formula. */
};
/* Global state for non-reentrant functions. */
struct drand48_data __libc_drand48_data;
int __drand48_iterate ( uint16 xsubi[3], struct drand48_data *buffer )
{
uint64 X;
uint64 result;

/* Initialize buffer, if not yet done. */
if (buffer->__init == 0)
{
buffer->__a = 0x5deece66du;
buffer->__c = 0xb;
buffer->__init = 1;
}

/* Do the real work. We choose a data type which contains at least
48 bits. Because we compute the modulus it does not care how
many bits really are computed. */

// this code needs brackets ! ( ( ) ) etc but where ?
X = (uint64) xsubi[2] << 32 | (uint32) xsubi[1] << 16 | xsubi[0];

result = X * buffer->__a + buffer->__c;

// use typecast to surpress warnings
xsubi[0] = uint16( result & 0xffff );
xsubi[1] = uint16( (result >> 16) & 0xffff );
xsubi[2] = uint16( (result >> 32) & 0xffff );

return 0;
}
sint32 __nrand48_r ( uint16 xsubi[3], struct drand48_data *buffer, sint32
*result )
{

/* Compute next state. */
if (__drand48_iterate (xsubi, buffer) < 0)
{
return -1;
}

/* Store the result. */
if (sizeof (unsigned short int) == 2)
{
// this code needs brackets ! ( ( ) ) etc but where ?
*result = xsubi[2] << 15 | xsubi[1] >> 1;
}
else
{
*result = xsubi[2] >> 1;
}

return 0;
}

sint32 nrand48 (uint16 xsubi[3])
{
sint32 result;

(void) __nrand48_r (xsubi, &__libc_drand48_data, &result);

return result;
}
int main()
{
uint16 state48[3];

printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );

// the output is:

// 1288336289
// 893806678
// 810173679
// 37620279
// 474250687

return 0;
}

Bye,
Skybuck.
Nov 14 '05 #1
2 1587
Jim
On Wed, 7 Jul 2004 15:51:07 +0200, "Skybuck Flying"
<no****@hotmail.com> wrote:
This code needs brackets but where ?

X = (uint64) xsubi[2] << 32 | (uint32) xsubi[1] << 16 | xsubi[0];
X = (uint64) (xsubi[2] << 32) | (uint32) (xsubi[1] << 16) | xsubi[0];
This code also needs brackets but where ?

*result = xsubi[2] << 15 | xsubi[1] >> 1;


*result = (xsubi[2] << 15) | (xsubi[1] >> 1);

These brackets will totally change your results.

Jim
Nov 14 '05 #2
On Thu, 08 Jul 2004 15:15:17 +1000, Jim <sp**@ihug.com.au> wrote:
On Wed, 7 Jul 2004 15:51:07 +0200, "Skybuck Flying"
<no****@hotmail.com> wrote:
This code needs brackets but where ?

X = (uint64) xsubi[2] << 32 | (uint32) xsubi[1] << 16 | xsubi[0];


X = (uint64) (xsubi[2] << 32) | (uint32) (xsubi[1] << 16) | xsubi[0];

Actually, it was correct before (assuming obvious meanings
for uint16,32,64 of course) and you've made it badly wrong except on a
(rare) system where signed int is at least 48 bits.
This code also needs brackets but where ?

*result = xsubi[2] << 15 | xsubi[1] >> 1;


*result = (xsubi[2] << 15) | (xsubi[1] >> 1);

These brackets will totally change your results.

No they won't. << and >> already have precedence above | and &.
And it was and remains UB unless signed int is more than 31 bits,
which is admittedly a good bit more common.
- David.Thompson1 at worldnet.att.net
Nov 14 '05 #3

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

Similar topics

9
by: Martin Waller | last post by:
Hello, I've been playing with the idea of just how to use an ASP page to provide a remote function call. In an ideal world this would be a web service but how can you do it if restricted to ASP...
18
by: Razvan | last post by:
Hi! What is the purpose of such a function ? int function(void)
12
by: Skybuck Flying | last post by:
Hello, I have a little piece of C source code which was probably made on a UNIX system. I would like to try and compile/build this source code on/for Windows XP with Visual C/C++ 6.0. The...
10
by: Skybuck Flying | last post by:
This is the source code for nrand48 from gnuwin32 libgw32c long int nrand48 (xsubi) unsigned short int xsubi; { long int result; (void) __nrand48_r (xsubi, &__libc_drand48_data, &result); ...
21
by: Skybuck Flying | last post by:
I am beginning to see why microsoft did not include this nrand SHIT !!!! 1. The documentation is SHIT 2. The implementation is SHIT =D IN OTHER WORDS UNIX IS SHIT !
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
1
by: russot00 | last post by:
I have 3 drop down menus that are used in a search to locate restaurants in a db. All of the drop down menus function, a search can be submitted with any combination of drop downs and the results are...
1
by: sxwend | last post by:
I am trying to use the following post results (http://www.thescripts.com/forum/thread189759.html) and add another requirement. I need to send the results to just the email addresses that the query...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.