1

I program in C++ on open-suse (with emacs) and on windows 8 (with VS). For some reasons, I'm forced to use the random() (man 3 random), of stdlib.h.

But, on VS, it appears random() doesn't exist and rand() returns me an int, and of course, I need a long int (like what random() returns).

Is there an alternative to have the same result in both system with the same seed ?

PanzerKadaver
  • 363
  • 3
  • 12
  • 1
    Use the `` header for both. `rand` doesn't even always return the full range of an `int`, though `int` is often the same size as `long int`. – chris Jan 15 '14 at 20:19
  • 1
    Might help: http://stackoverflow.com/questions/15173238/windows-equivalent-of-posix-srandom-and-random-functions – TypeIA Jan 15 '14 at 20:20

1 Answers1

2

The C++11 standard added <random> which gives you a wide range of different psuedo-random number generators.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • Yes i saw that, but i need an exact equivalent (for the same seed, the same 'random' number list) – PanzerKadaver Jan 15 '14 at 20:36
  • 2
    @PanzerKadaver Since `random()` is a non-standard extension, you won't find one. If you want portable code, you should stick to using functions that *are* standard. – Zac Howland Jan 15 '14 at 20:42
  • 1
    If it is important then just [copy the code](http://code.woboq.org/userspace/glibc/stdlib/random.c.html). Don't forget about the license terms :) – Hans Passant Jan 15 '14 at 20:52
  • @HansPassant There are many thing in that code make it doesn't compile and i don't understand why (i'm current learning Cpp). – PanzerKadaver Jan 15 '14 at 20:57
  • @HansPassant It wouldn't be that simple as it (especially that implementation) utilizes functions that are also non-standard (and do not exist in VS's implementation). He would have to port it over, and test it heavily (since his requirement was to have the same results with the same seed values). – Zac Howland Jan 15 '14 at 21:21