r/C_Programming • u/Parking_Impress539 • 2d ago
Review Help with seed generation for RNG.
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
i8 RANDOM_SEED(){
static i8 seed = 0;
static i8 init = 0;
if (init) return seed;
FILE *pF = fopen("SEED_FILE", "rb");
if (pF) {
fread(&seed, sizeof(i8), 1, pF);
}
else {
i8 created = 0;
srand((u32)time(NULL));
seed = (i8)(rand() % 246 - 128);
do {
pF = fopen("SEED_FILE", "wb");
if(pF){
fwrite(&seed, sizeof(i8), 1, pF);
created = 1;
}
} while(created != 1);
}
fclose(pF);
pF = NULL;
init = 1;
return seed;
}
I'm trying to make a seed generator. How it works: you call the function and it creates a seed file with a raw binary value inside it that cannot be read by a human. The program reads it and produces a seed that will always be the same for one computer but will vary from computer to computer. I will use it to make a random number generator whose output is constant on the same machine but differs across machines. Can you tell me if this is efficient, or am I overdoing, underdoing, or overcomplicating things? And also I want to make it memory efficient.
I am making this to clarify my concept on file reading - writing and RNG.
6
u/tstanisl 1d ago
8bit is not especially great amount of entropy to work with. Good quality generators use thousands. For just statistically correct noise generators (which rand() IS NOT) 64 bit is usually enough. I don't see the point of storing to a file. Just dump it to logs and add an option for user to pass it explicitly. Otherwise use date or /dev/random.
3
u/MFFVD 1d ago
while(created != 1)? that seems a bit strange for errorhandling
5
u/mjmvideos 1d ago
Explain your thinking on “cannot be read by a human”. Any file in a file system can be read by a human.
9
u/No_External_4869 1d ago
3
u/sciencekm 1d ago
If you want something that is unique to a computer, you should get all the MAC addresses, the SMBIOS ID and all the drive IDs, then hash all of those.
3
u/wwabbbitt 1d ago edited 1d ago
There is no guarantee that this seed you generate will be unique to the computer and remain the same.
Since its seeded by time(NULL), two computers running this at the same time will have the same seed.
If you rerun this program on any computer,
the new seed file generated will overwrite the old seed fileand your rng output will then be different within the same computer.EDIT: Just saw that it checks for existing seed file and won't overwrite. In any case, you'd want the seed file to be more resilient to being edited or deleted.
The appropriate way to achieve what you want is to seed your RNG using the unique machine id provided by your OS. On windows you can use the registry item HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid on linux you can read it from /etc/machine-id
1
u/Ordinary-Pirate7852 1d ago
do {
pF = fopen("SEED_FILE", "wb");
if(pF){
fwrite(&seed, sizeof(i8), 1, pF);
created = 1;
}
} while(created != 1);
why are we looping here
feels kinda wrong calling fopen on an already open file (i might be wrong on this)
1
u/gremolata 1d ago
What case that do-while loop is meant to address?
1
u/MarekKnapek 1d ago
No point of storing just 1Byte if randomness into a file. Whatever you do, your PRNG will start with one of just 256 possibilities. Too few. I can crack your randomness if I try 256 times. Decide what you want to use: Maximal C portability, or real world. In case of real world, just read the /dev/random file or call the CryptGenRandom/BCryptGenRandom/RtlGenRandom function.
1
u/al2o3cr 1d ago
Nitpick: this will only return numbers between -128 and 117. Is that on purpose?
1
u/FedUp233 1d ago
If you’re going to store something on one of the drives in the file system, why not just use the drive’s UUID and hash that? There should never be any duplicates.
Or, I’m not that familiar with the security chip that works with secure boot but if there is an api to use that you could just generate a UUID and store that in the secure storage which would essentially be there for the life of the of the pc even if the cpu were replaced or drives replaced.
1
u/0xMattB 1d ago
Why are you typedef'ing common types?