UNIX's nrand48() routine for Windows/Visual C/C++ 6.0 ?

S

Skybuck Flying

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 problem is with nrand48(). Apperently the UNIX system has this routine
nrand48() in <stdlib.h>

Apperently Visual C/C++ 6.0 doesn't have this routine in it's library ?

I have no further idea how to program works so I would like to keep changes
to a minimum.

So the question becomes:

What is the best replacement for nrand48() for Visual C/C++ 6.0 ?

Bye,
Skybuck.
 
J

Jens.Toerring

Skybuck Flying said:
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 problem is with nrand48(). Apperently the UNIX system has this routine
nrand48() in <stdlib.h>
Apperently Visual C/C++ 6.0 doesn't have this routine in it's library ?
I have no further idea how to program works so I would like to keep changes
to a minimum.
So the question becomes:
What is the best replacement for nrand48() for Visual C/C++ 6.0 ?

Dangerously near to off-topic;-) If RAND_MAX is 2147483647 (2^31 - 1)
on your system you can probably replace nrand48() by the standard C
function rand() without any changes (unless the algorithms used in the
program rely also on some other properties of the nrand48() function,
see

http://www.opengroup.org/onlinepubs/009695399/functions/drand48.html

Otherwise you have to look out for some other function that returns
uniformely distributed pseudo-random numbers in the interval [0,2^31),
I guess someone in a MS-programming group will know what to use.

Regards, Jens
 
R

Randy Howard

What is the best replacement for nrand48() for Visual C/C++ 6.0 ?

As is almost always the case, for standards compliant code, use rand/srand,
or if you want a guarantee on the quality of a PRNG, use one known to be
both portable and of sufficient quality. "Mersenne Twister" is a good
place to start.
 
S

Skybuck Flying

Skybuck Flying said:
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 problem is with nrand48(). Apperently the UNIX system has this routine
nrand48() in <stdlib.h>
Apperently Visual C/C++ 6.0 doesn't have this routine in it's library ?
I have no further idea how to program works so I would like to keep changes
to a minimum.
So the question becomes:
What is the best replacement for nrand48() for Visual C/C++ 6.0 ?

Dangerously near to off-topic;-) If RAND_MAX is 2147483647 (2^31 - 1)
on your system you can probably replace nrand48() by the standard C
function rand() without any changes (unless the algorithms used in the
program rely also on some other properties of the nrand48() function,
see

http://www.opengroup.org/onlinepubs/009695399/functions/drand48.html

Otherwise you have to look out for some other function that returns
uniformely distributed pseudo-random numbers in the interval [0,2^31),
I guess someone in a MS-programming group will know what to use.

Yes, I believe the program relies on nrand48 special properties...
 
J

Jens.Toerring

Skybuck Flying said:
Skybuck Flying said:
What is the best replacement for nrand48() for Visual C/C++ 6.0 ?

Dangerously near to off-topic;-) If RAND_MAX is 2147483647 (2^31 - 1)
on your system you can probably replace nrand48() by the standard C
function rand() without any changes (unless the algorithms used in the
program rely also on some other properties of the nrand48() function,
see

http://www.opengroup.org/onlinepubs/009695399/functions/drand48.html

Otherwise you have to look out for some other function that returns
uniformely distributed pseudo-random numbers in the interval [0,2^31),
I guess someone in a MS-programming group will know what to use.
Yes, I believe the program relies on nrand48 special properties...

You mean that it uses a linear congruential algorithm and 48-bit
integer arithmetic, so it is not just used as a normal generator
of 32-bit pseudo-random numbers but depends on the exact sequence
the numbers are produced? In that case you may have to write your
own version, the algorithm is described on the page with the URL
I posted.
Regards, Jens
 
S

Skybuck Flying

Skybuck Flying said:
What is the best replacement for nrand48() for Visual C/C++ 6.0 ?

Dangerously near to off-topic;-) If RAND_MAX is 2147483647 (2^31 - 1)
on your system you can probably replace nrand48() by the standard C
function rand() without any changes (unless the algorithms used in the
program rely also on some other properties of the nrand48() function,
see

http://www.opengroup.org/onlinepubs/009695399/functions/drand48.html

Otherwise you have to look out for some other function that returns
uniformely distributed pseudo-random numbers in the interval [0,2^31),
I guess someone in a MS-programming group will know what to use.
Yes, I believe the program relies on nrand48 special properties...

You mean that it uses a linear congruential algorithm and 48-bit
integer arithmetic, so it is not just used as a normal generator
of 32-bit pseudo-random numbers but depends on the exact sequence
the numbers are produced? In that case you may have to write your
own version, the algorithm is described on the page with the URL
I posted.

Well I rather not write that stuff ;) so a search with google for nrand48
implementation turned up this one

http://gnuwin32.sourceforge.net/packages/libgw32c.htm

So tomorrow I will try it out... hopefully the executables won't get to big
by including it... low on disk space ;) =D though I'll be happy if the all
compile so I can try them out ;)

Bye,
Skybuck.
 
S

Skybuck Flying

Randy Howard said:
As is almost always the case, for standards compliant code, use rand/srand,
or if you want a guarantee on the quality of a PRNG, use one known to be
both portable and of sufficient quality. "Mersenne Twister" is a good
place to start.

The lrand48() and nrand48() functions return non-negative, long integers,
uniformly distributed over the interval [0,231].

Can the "Mersenne Twister" random number generator be used to:

"return non-negative, long integers, uniformly distributed over the interval
[0,231]."

???

Bye,
Skybuck.
 
S

Skybuck Flying

Skybuck Flying said:
What is the best replacement for nrand48() for Visual C/C++ 6.0 ?

Dangerously near to off-topic;-) If RAND_MAX is 2147483647 (2^31 - 1)
on your system you can probably replace nrand48() by the standard C
function rand() without any changes (unless the algorithms used in the
program rely also on some other properties of the nrand48() function,
see

http://www.opengroup.org/onlinepubs/009695399/functions/drand48.html

Otherwise you have to look out for some other function that returns
uniformely distributed pseudo-random numbers in the interval [0,2^31),
I guess someone in a MS-programming group will know what to use.
Yes, I believe the program relies on nrand48 special properties...

You mean that it uses a linear congruential algorithm and 48-bit
integer arithmetic, so it is not just used as a normal generator
of 32-bit pseudo-random numbers but depends on the exact sequence
the numbers are produced? In that case you may have to write your
own version, the algorithm is described on the page with the URL
I posted.

I am not sure what the program depends on. The safest bet would be to have
an nrand48() function on windows which returns exactly the same numbers as
the nrand48() function on unix.

I see a number of possibilities:

1. Find the source code for nrand48() and have it compile/work on windows.
This probably requires a lot of other source for rand48() etc...

2. Use another random number generator like "Mersenne Twister"... maybe it
can also produce:

"return non-negative, long integers, uniformly distributed over the interval
[0,2^31]."

The best thing would be if it were the same numbers but that's probably
unlikely.

Maybe the Mersenne Twister is even a better random number generator than
nrand48 ;)

3. Write an nrand48() implementation from scratch ;) <- I rather not do that
since I don't know anything about random number generators or 48 bit
arithmetic, and I suck at statistics =D

I also tried linking the library 'libgw32c.a' in visual c, but that did not
work. I think the library is not ment for visual c... maybe it's ment for
gnu c compilers.

The source code of the library 'libgw32c.a' is also available so I might
succeed in extracting the necessary code for the nrand48() functions and any
other needed function and then putting that in a seperate file ;)

I would still have to find a way to make sure that it procedures the same
numbers as on unix... I should least have some indication that it s working
correctly.

Maybe I am taking it a little bit to extremes... since I know that a c
compiler is just an implementation of the c language... and each c compiler
can have errors or produce code that proceduces different results. So in
other words a unix c program might produce other results than the same c
program on windows.

Bye,
Skybuck.
 
S

Skybuck Flying

I might be that the people that made the source were pretty smart...

They generated some random numbers and stored them in a file called
'randfile'...

The source also uses a RAND_FILE define or something...

I am not sure if I should make this:

#define RAND_FILE "randfile"

or that I should use a different file to prevent the "randfile" from getting
overwritten.
 
S

Skybuck Flying

Yes I inspected rand.c it seems it has special functions for generating it's
own random numbers based on the random numbers in the file randfile...

The random numbers of randfile are first read in a lookup table and then
later used :)

The following projects/executables are compiling/building to executables:

1. Decode.exe
2. Encode.exe
3. Extract.exe
4. MakeGen.exe
5. MakeLDPC <- fails, needs nrand48()
6. MakePCHK.exe
7. Mod2ConvertTest.exe <- fails, needs nrand48()
8. Mod2DenseTest.exe
9. Mod2SparseTest.exe
10. PrintGen.exe
11. PrintPCHK.exe
12. RandSrc.exe <- fails, needs nrand48()
13. RandTest.exe <- fails, needs nrand48()
14. Transmit.exe <- fails, needs nrand48()
15. Verify.exe

I am not sure how important MakeLDPC is at this point...

Transmit seems also pretty important for introducing noise...

Bye,
Skybuck.
 
R

Randy Howard

Randy Howard said:
As is almost always the case, for standards compliant code, use rand/srand,
or if you want a guarantee on the quality of a PRNG, use one known to be
both portable and of sufficient quality. "Mersenne Twister" is a good
place to start.

The lrand48() and nrand48() functions return non-negative, long integers,
uniformly distributed over the interval [0,231].

Can the "Mersenne Twister" random number generator be used to:

"return non-negative, long integers, uniformly distributed over the interval
[0,231]."

I don't see any reason why not. The source is freely available on the web,
check it out and see for yourself.
 
N

Nitin Bhardwaj

Skybuck Flying said:
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 problem is with nrand48(). Apperently the UNIX system has this routine
nrand48() in <stdlib.h>

Apperently Visual C/C++ 6.0 doesn't have this routine in it's library ?

I have no further idea how to program works so I would like to keep changes
to a minimum.

So the question becomes:

What is the best replacement for nrand48() for Visual C/C++ 6.0 ?

Bye,
Skybuck.

You are doing a thing horribly wrong. Visual C++ Compiler has both an
ANSI confirming C Compiler as well as C++ Compiler.It relies on the
extension of the source file, which one is invoked by 'cl.exe' .So
before compiling your nrandxx Source file of GNU Win32, please do
change the file extension to *.c

The MSDN Library for VC++ mentions this point !!

BTW The C Compiler in VC++ supports the old K&R style of function
definition.Here it gave you error because your file extension was
*.cpp and this invoked the C++ cimpiler, which does not support old
K&R definition style.

HTH
Nitin Bhardwaj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top