r/cprogramming • u/78900sea • Jul 13 '26
Casino on c
Thanks to everyone . A special thanks to those who suggested the /dev/random - this very help for my tasks.
4
Upvotes
r/cprogramming • u/78900sea • Jul 13 '26
Thanks to everyone . A special thanks to those who suggested the /dev/random - this very help for my tasks.
10
u/sciencekm Jul 13 '26
Rand() is the worst source of random data for a casino game. Rand() only looks random. It is not random. Once you have seen a few output, you will be able to predict all the remaining output. You don't want that on a casino.
If you are creating a Windows app, use the CryptGenRandom() API. If you are using Linux/Unix/BSD/MacOS, read random numbers from the /dev/urandom device file.
8
u/soundman32 Jul 13 '26
Build you game with rand() then use a better generator. You will find that the randomness is the least of your problems.
4
u/nerd5code Jul 13 '26
To everyone saying
rand/srand, ffs no. Its sole advantage is that your program will probably link and executebsuccessfully if you use it; there are almost no requirements for it beyond that, and just about any reliance on details there would be a bad idea.There are a few easyish options, but those depend on platform, and there are two aspects to good random numbers that you might need to care about: entropy source(s) and mixing/hashing/RNGulating algorithm(s).
On most modern Linux and Unix (probably Cygwin also), there's at least one random data “device.” E.g., on Linux, /dev/random is more strictly-noisy but much slower; /dev/urandom is fine for general use and much faster. IIRC BSD goes the other way with /dev/srandom (slow/secure) and /dev/random (faster), but every OS is a bit different on this front on account of random devices not being in any of the big standards yet.
You can pull all of your random data from a device, but for “secure random” devs like Linux /dev/random, frequent use will quickly exhaust the entropy gathered by the device driver thus far and slow you down to a snail's pace. (/dev/urandom can match interactive speeds comfortably, but I still wouldn't put it in a critical path.) For device-based approach,
#definefor the device name, if one isn't given,readorfreada byte-buffer, andmemcpyit in from the byte buffer.You can either reread or shuffle the byte-buffer once it's exhausted by random-grabbing, and if you hash outputs, you only really need to advance by a single byte for each read.
Alternatively, you can read a single seed value, and use a software RNG to generate a random/-ish sequence of successive values from that. XORShift variants are very easy to implement with acceptable results (not universally great, not sufficient to run an actual casino, but for your likely purposes), although you need to take care to avoid all-zero seeds without ruling out all-zero outputs entirely. Feedback-hashing can also potentially give you a RNG, but you typically need a higher-test hash to do this well. (Useful component of a RNG, either way.)
The seed value can come from (e.g.)
rand,time(you get one unique-ish value per second, mind you) and finer-grained clocks/timers,drand48(SVID, XPG, POSIX.1),getpid()orgettid(),errnoand other static/TLS data,__builtin_frame_address()and__builtin_return_address(),__FILE__and__LINE__and__COUNTER__and__DATE__and__TIME__and predefines, or__asm__("… .incbin …"),#embed,objcopy, or XBM/XPM-style conversion to#includeable byte-array data—od,sed,awk).Additionally, recent-ish Bash gives you
$RANDOMas a special parameter variable (does not show up in exports, AFAIK) (or rather, only a single random value will be propagated to child processes, as captured at instant ofexport), which you can use to set a quick-and-dirty extrinsic seed via argv or environ.(Entropy gathering/mixing and sequence generation are both components of what most random-device drivers do; it's basically just a RNG library whose API uses
open+readrather than its own frontend.)If you need your sequence of numbers to be reproducible, so you can replay exact scenari…os? scenarii? scenariæ? scenariahahahahatrices for testing or amusing friends/family/QA, you need to be able to set a specific seed via environment or command-line args.
If you don't need reproducibility, you can mix in any number of entropy sources you please, and use them for initialization (some of these might require some warm-up time, and thus won't be available immediately), and as occasional mix-ins to discourage the RNG from cycling noticeably or spiraling incelwards, and to help paper over what might otherwise be reverse-engineerable aspects of the output sequence.
If you need good entropy immediately without relying on peculiarities of OS (well, beyond there being some sort of multiprogramming or TSR-supportive jobby with something socketlike or memory-shareable, which is a good bet most of the time), you can set up a service to provide an analogue to what /dev/[u]random would give you, or possibly just direct applications to open that device or some other file directly when it's available & configured.
In terms of a single, pre-fab function,
drand48orarc4randomis probably your best bet on Unix, Cygwin, M/Sys2, DJGPP, EMX, Interix/SFU, or other non-DOS/2Win/NT OSes and Unix-alike AEEs.GNU/Linux offers a
getrandomsyscall (kernel 3.17+), or C-exposed API via Glibc (2.25+ on Linux-3.17+ via<sys/random.h>; idk about Bionic or others; always check for errors likeENOSYS/ENOTSUP) as a more direct means of accessing the /dev/[u]random sources.WinAPI per se has things like
ProcessPrng,RtlGenRandom, and [B]CryptGenRandom; I think the first is most preferred but I try to ngaf about WinAPI.4
u/Immediate-Food8050 Jul 13 '26
rand is all the standard library gives you. You can easily implement xorshift yourself. At that point your choice of seed is all that will really matter for something like this.
2
u/[deleted] Jul 13 '26
if you want it accurate to a real casino, used fixed literal values to ensure predictable profit margins that balance the illusion of winning for your customer ...
i mean uh yeah rand's likely your best bet unless you want to get into the weeds of OS specific stuff
1
u/v_maria Jul 13 '26
Why not rand
2
u/mc_pm Jul 13 '26
The trick is you're going to want to program your own functions - using rand() - to handle rolling dice, dealing cards, etc.
2
u/Interesting_Buy_3969 Jul 13 '26
rand()is the standard way to get a random number. However, rand is usually implemented as pseudo-random generator, which means numbers that it outputs are not truly random. Therefore a standard, common practice is to seed it with a non-compile-time value, for some instance, the current time. The functionsrandcan be leveraged to achieve "true randomisation" like that:And then you can call
rand(); to get a random number in a specific range, create your own function similar to that written by u/my_password_is______ . Ensure that before any sensitive data is generated at runtime, the generator is seeded properly.As an alternative, on most POSIX systems you may just read
/dev/urandomand doing so you don't have to seed anything.1
u/gotgoodendinginSoTC Jul 13 '26
https://rosettacode.org/wiki/Pseudo-random_numbers/Splitmix64
-1
u/my_password_is______ Jul 13 '26
https://www.geeksforgeeks.org/c/generating-random-number-range-c/
-3
u/Abrissbirne66 Jul 13 '26
srand