# Thread: Psuedo random generator with min max

1. ## Psuedo random generator with min max

Can't find my old thread on min max so asking again, I made this function which should be as close to random numbers getting on a computer without internet connection for that random.com or whatever it was, only I forgot how to use the 1 or 0 to produce a larger (or smaller) number within a range.
Code:
```long mcc_rnd( time_t *ctx, long min, long max ) {
/* With bit unitialised it should be much harder to predict if 1 or 0 will recieved */
time_t bit, seed = time(NULL);
long val;
if ( ctx ) bit = *ctx;
if ( !bit ) bit = 1;
val = (seed & bit) ? 1 : 0;
bit <<= 1;
if ( ctx ) *ctx = bit;
if ( min > max ) min = max;
if ( min != max ) return val ? min;
if ( max ) return val ? max : 0;
}```
Would appreciate being reminded of how to do so.

2. After a google search I found a copy of what I used (forgot I had put one in the mitsy code) but that wound up crashing so now I went with something different:
Code:
```typedef clock_t mcc_rnd_t;
long mcc__rnd( mcc_rnd_t *ctx, long min, long max ) {
/* With bit unitialised it should be much harder to predict
* if 1 or 0 will be recieved */
mcc_rnd_t bit, seed = clock();
long val;
if ( ctx ) bit = *ctx;
if ( !bit || bit > seed ) bit = 1;
val = (seed & bit) ? 1 : 0;
bit <<= 1;
if ( ctx ) *ctx = bit;
if ( min > max ) min = max;
if ( min != max ) {
val *= bit;
if ( val > max ) val = max;
val -= min;
return ( val < min ) ? min : val;
}
return val ? max : min;
}
#define mcc_rnd( ctx ) mcc__rnd( ctx, LONG_MIN, LONG_MAX )```
Not as flexibile as I'd like but it'll do, any suggestions for more flexible results than the below are welcome
Code:
```gcc -Wall -o "bimath" "bimath.c" && "./bimath"
num = 4, val = 0
num = 16, val = 8
num = 0, val = 32
num = 0, val = 128
num = 1024, val = 0
num = 4096, val = 0
num = 16384, val = 8192
num = 0, val = 0
num = 16, val = 0
num = 0, val = 0
num = 256, val = 128
num = 1024, val = 0
num = 4096, val = 0
num = 16384, val = 8192
num = 4, val = 0
num = 16, val = 0
num = 64, val = 0
Compilation finished successfully.```
And yes those results do change upon every run of the test code

3. Managed to make it a bit more random:
Code:
```long mcc__rnd( mcc_rnd_t *ctx, long min, long max ) {
/* With bit unitialised it should be much harder to predict
* if 1 or 0 will be recieved */
mcc_rnd_t bit = 1, seed = clock(), mov = 0;
long val;
if ( ctx ) mov = *ctx;
bit <<= mov++;
if ( !bit || bit > seed ) {
bit = 1;
mov = 0;
}
val = (seed & bit) ? 1 : 0;
bit <<= 1;
if ( ctx ) *ctx = mov;
if ( min > max ) min = max;
if ( min != max ) {
bit = ~((~0u) << mov);
val *= (clock() & bit);
if ( val > max ) val = max;
val -= (min >= 0) ? min : -min;
return ( val < min ) ? min : val;
}
return val ? max : min;
}```
Code:
```gcc -Wall -o "bimath" "bimath.c" && "./bimath"
num = 0, val = 0
num = 7, val = 1
num = 0, val = 0
num = 0, val = 58
num = 335, val = 0
num = 1378, val = 0
num = 0, val = 0
num = 0, val = 9604
num = 0, val = 0
num = 9, val = 7
num = 55, val = 21
num = 198, val = 67
num = 0, val = 465
num = 0, val = 1504
num = 9746, val = 0
num = 1, val = 0
num = 6, val = 0
Compilation finished successfully.```

4. This is the closest to unpredictable as I can get it, while one can predict to an extent what range a number will fall in, those numbers can be wildly different in magnitude so trying to brute force in a real world scenario would be quite difficult I imagine.
Code:
```long mcc__rnd( mcc_rnd_t *ctx, long min, long max ) {
/* With bit unitialised it should be much harder to predict
* if 1 or 0 will be recieved */
mcc_rnd_t bit = 1, seed = time(NULL), mov = 0;
long val;
if ( ctx ) mov = *ctx;
bit <<= mov++;
if ( !bit || bit > seed ) {
bit = 1;
mov = 0;
}
val = (seed & bit) ? 1 : 0;
bit <<= 1;
if ( ctx ) *ctx = mov;
if ( min > max ) min = max;
if ( min != max ) {
seed *= clock();
bit = ~((~0u) << mov);
val = (seed & bit);
if ( val > max ) val = max;
val -= (min >= 0) ? min : -min;
return ( val < min ) ? min : val;
}
return val ? max : min;
}```
Code:
```gcc -Wall -o "bimath" "bimath.c" && "./bimath"
num = 1, val = 1
num = 12, val = 6
num = 17, val = 10
num = 168, val = 1
num = 102, val = 191
num = 2596, val = 726
num = 1083, val = 1172
num = 32249, val = 16043
num = 194309, val = 47031
num = 733379, val = 397596
num = 2288613, val = 1617047
num = 4556640, val = 6318009
num = 31990491, val = 197428
num = 141549053, val = 42647126
num = 339883433, val = 410515035
num = 0, val = 989835645
num = 2, val = 1
num = 14, val = 7
num = 51, val = 5
num = 10, val = 99
num = 122, val = 211
num = 2616, val = 657
num = 9295, val = 1192
num = 15974, val = 32536
num = 219261, val = 71983
num = 758331, val = 422548
num = 1559545, val = 1223762
num = 16410484, val = 1394637
num = 27067119, val = 28828488
num = 37723754, val = 73039555
num = 80173452, val = 115489253
num = 0, val = 780062943
num = 0, val = 0
num = 12, val = 6
num = 29, val = 15
num = 52, val = 13
num = 498, val = 75
num = 3170, val = 1211
num = 1657, val = 1746
num = 24720, val = 16617
num = 170574, val = 23296
num = 824510, val = 488727
num = 3010083, val = 577148
num = 15009850, val = 8382611
num = 25666485, val = 27427854
num = 237649712, val = 4530057
num = 583850667, val = 82295556
num = 0, val = 1233802879
num = 0, val = 1
num = 7, val = 0
num = 11, val = 29
num = 212, val = 45
num = 914, val = 68
num = 425, val = 514
num = 3097, val = 3275
num = 1495, val = 18057
num = 139246, val = 123040
num = 531038, val = 383760
num = 660686, val = 324903
num = 2510476, val = 4271845
num = 37914698, val = 6121635
num = 48571333, val = 83887134
num = 896327399, val = 430088089
num = 0, val = 1546279611
num = 1, val = 0
num = 15, val = 0
num = 58, val = 12
num = 227, val = 60
num = 577, val = 154
num = 2136, val = 177
num = 4630, val = 4808
num = 27871, val = 19768
num = 108189, val = 91983
num = 762125, val = 426342
num = 2276132, val = 1604566
num = 16037268, val = 2782790
num = 1109842, val = 2871211
num = 170731792, val = 107145666
num = 248497291, val = 319128893
num = 0, val = 898449503
num = 0, val = 0
num = 1, val = 2
num = 25, val = 4
num = 20, val = 109
num = 989, val = 54
num = 3398, val = 1439
num = 5892, val = 6070
num = 37058, val = 28955
num = 35634, val = 19428
num = 427426, val = 91643
num = 1941433, val = 1269867
num = 11508265, val = 4881026
num = 16880793, val = 18642162
num = 52285015, val = 87600816
num = 1062529134, val = 24103111
num = 0, val = 638739522
num = 3, val = 1
num = 15, val = 0
num = 26, val = 19
num = 236, val = 69
num = 871, val = 448
num = 1009, val = 1098
num = 7688, val = 7866
num = 55060, val = 6086
num = 77945, val = 4306
num = 92727, val = 281232
num = 2278300, val = 1606734
num = 14949633, val = 1695155
num = 46831117, val = 16799423
num = 188182742, val = 89280815
num = 590924347, val = 89369236
num = 0, val = 1099613355
num = 1, val = 0
num = 8, val = 1
num = 3, val = 21
num = 151, val = 112
num = 620, val = 197
num = 487, val = 576
num = 7433, val = 7522
num = 55161, val = 14290
num = 258448, val = 111170
num = 912384, val = 52313
num = 1754825, val = 1419042
num = 1171680, val = 4694418
num = 58637225, val = 26844162
num = 97564185, val = 132879986
num = 641568994, val = 140013883
num = 0, val = 1651813113
num = 3, val = 1
num = 11, val = 4
num = 6, val = 31
num = 218, val = 51
num = 853, val = 430
num = 4061, val = 54
num = 7000, val = 7178
num = 22138, val = 14035
num = 227120, val = 22409
num = 954695, val = 283129
num = 3181495, val = 748560
num = 1926784, val = 3688153
num = 7299312, val = 10822050
num = 14433209, val = 49749010
num = 523122217, val = 21567106
num = 0, val = 530256114
num = 3, val = 1
num = 0, val = 2
num = 50, val = 4
num = 134, val = 95
num = 513, val = 90
num = 3541, val = 1582
num = 6482, val = 6571
num = 29901, val = 21798
num = 234171, val = 29460
num = 225627, val = 414132
num = 691058, val = 19492
num = 10257890, val = 3630651
num = 34169003, val = 2375940
num = 108411764, val = 9509837
num = 885536228, val = 419296918
num = 0, val = 2037043551
num = 1, val = 0
num = 8, val = 2
num = 8, val = 1
num = 220, val = 53
num = 87, val = 176
num = 1067, val = 1156
num = 4006, val = 4095
num = 20305, val = 12202
num = 238374, val = 91096
num = 574428, val = 91367
Compilation finished successfully.```

5. Why are you basing your rng() on time() and clock().

Which, depending on how often you call them, return
- the same as last time
- +1 from last time
- always some number greater than last time.

If you want crude-but-effective (for student homework), then use a LCG
Linear congruential generator - Wikipedia

If you want statistically sound, or cryptographically sound randomness, then that takes more work.

6. Originally Posted by Salem
Why are you basing your rng() on time() and clock().

Which, depending on how often you call them, return
- the same as last time
- +1 from last time
- always some number greater than last time.

If you want crude-but-effective (for student homework), then use a LCG
Linear congruential generator - Wikipedia

If you want statistically sound, or cryptographically sound randomness, then that takes more work.
I wanted a thread safe version that was not easy to predict, I don't need impossible just hard enough that outside a test scenario it would be pretty hard to predict without first finding out when the function is called in the 1st place, for games this would be sufficient and for testing my bignum functions it is also sufficient (those numbers were actually passed through my checker function to see if they matched up, as you'll note by the lack of further output they did match).Since it always changes the seed the randomness can only be predicted after a few rounds of it being used. Feel free to stress test it though