r/cprogramming • u/ZenMemeProvider • Jan 21 '26
How to compile C on Linux to behave exactly like Windows ?
rand() returns different values on Windows and Linux, even when using the same srand() seed. I assume this is compiler- related.
For college homework testing, how can I make rand() behave the same as on Windows without dual-booting?
27
Upvotes
49
u/Sosowski Jan 21 '26
standard does not specify rand() implementation. you need to roll your own.
23
u/[deleted] Jan 21 '26
You need to use same library, which is generally compiler specific. Both GCC and Clang have Windows options. The Microsoft compiler doesn't work on Linux.
The other option is, implement your own srand and rand and use that instead. Which will probably be easier.
15
u/mustbeset Jan 21 '26
Why did you need the same random numbers?
For testing just use a large array with "random" numbers.
8
u/duane11583 Jan 21 '26
rand is nothing more the a simple equation.
see this: https://en.wikipedia.org/wiki/Linear_congruential_generator
you just need to create your own random generator and use it on both systems.
12
u/thefeedling Jan 21 '26
If anything, avoid
rand()You can use some better and more modern engines, based on pcg or xoshiro256
6
u/rphii_ Jan 21 '26
make your own
5
u/mprevot Jan 21 '26
Just use a Mersenne twister in both.
5
u/sessamekesh Jan 21 '26
For most things, you'll get "exactly" the same only as far as the API contract is upheld. For rand, the contract includes a couple properties (no number is more likely than another, after a sufficiently large number of samples all numbers are about as equally likely, the next number is not predictable...) but it does not guarantee the same sequence of outputs given a seed.
If you want determinism (which can be helpful for testing!) you have a couple options - the easiest in this case is to probably pull in a non-standard rand implementation (I like Mersenne twisters and there's a zillion good libraries out there) and use that.
3
u/knowwho Jan 21 '26
If you actually need reproducible input for your tests, don't rely on
rand()with a specific seed. The simplest thing is probably to hard-code a big array of dummy inputs and expected outputs.3
u/SmokeMuch7356 Jan 21 '26
It indeed varies by implementation.
rand()is only required to generate the same pseudorandom sequence for a given seed, but there's no requirement as to the sequence itself.You'd either need to find a third-party PRNG that generates the same sequences across implementations, or roll your own.
3
u/[deleted] Jan 21 '26 edited Jan 21 '26
I usually use https://www.pcg-random.org
3
u/wsbt4rd Jan 21 '26
Read about about "POSIX".
You'll learn a lot doing this. Not sure you'll solve the quest, but you'll LEARN A LOT
3
u/DawnOnTheEdge Jan 22 '26
MinGW-w64 on Windows might behave more like GCC on Linux, but you probably need to write your own portable PRNG. A linear-congruential generator might be the simplest, but xor-shift and Mersenne-twister are both popular algorithms.
2
u/sol_hsa Jan 21 '26
There's a bunch of differences that you learn the hard way when doing portable software, and rand is one of them. msvc's rand is a particularly bad implementation so it's better to use something else in any case.
Sometimes you have no option, in which case you just have to reimplement rand() in non-msvc environments to behave the same way.
2
u/beleniak Jan 21 '26
You can always write your own random
2
u/kombiwombi Jan 21 '26
PJ Plauger (the chair of the ANSI C committee) wrote a sample C library. You could look at his basic implementation of srand() and rand().
This person has uploaded the code
https://github.com/wuzhouhui/c_standard_lib
2
u/Pesciodyphus Jan 21 '26
here:
https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_useSource:
Microsoft Visual/Quick C/C++
modulus m: 231
multiplier a: 214013 (0x343FD)
increment c: 2531011 (0x269EC3)
bits: 30... 16
2
u/Efficient-Ad-4300 Jan 21 '26
Another option would be to use a virtual machine instead of dual booting. A virtual machine is basically a program that allows you to run a different operating system inside it. If you are on Windows and want to host a Linux VM you can use a free VM host like VMware or virtual box. I don't have much experience with hosting a windows VM on Linux but I think a popular Linux VM software is called kvm.
1
u/jeffbell Jan 22 '26
At the very least wrap it in a function. You could add a test mode flag that causes it to give the same result.
1
u/dariusbiggs Jan 22 '26
It won't, there are differences between them due to the underlying operating systems and even the compilers used (if they're different).
The ones I ran into many decades ago.
1
u/RRumpleTeazzer Jan 22 '26
you need to write your own rand(), or take a library and use a specific algorithm.
1
u/qruxxurq Jan 22 '26
Roll your own sequence generator, so that given a seed, it will reproduce the same sequence each time. Most of the PRNG algorithms already have that property; you just have to use one.
If you think about this for more than 15 seconds, it will become obvious why you shouldn't be using
rand(3).1
u/Key_River7180 Jan 23 '26
uuh, change kernel I guess?
1
u/andycwb1 Jan 23 '26
Use the random functions from OpenSSL, they should be the same.
1
u/Tuhkis1 Jan 25 '26
Just avoid the standard library as much as you can especially if you need it to avt the same way on all platforms