You need to write your generator, because there is no possibility to add a library with a built-in. Can you please tell me the link or formula that you can use on C, where there will be random numbers between the minimum and maximum. Type int and double .

PS I am writing under OpenCL. There are no built-in ready-made libraries with this. There are options, but there is a little more difficult. Did anyone have experience writing a primitive PRNG without any built-in functions?

  • You can not use cstdlib? - pinguin
  • What source of entropy do you plan to use? Or do you still need a pseudo-random number generator? - vp_arth
  • @vp_arth pseudo-random, forgot to add. - Fresto
  • one
    You can take the algorithm from java.util.Random - vp_arth
  • @vp_arth Thank you, I will study now - Fresto

5 answers 5

Once upon a time (before C ++ 11) I wrote on the second volume of Knut for 32 and 64 bits - if it helps ...

 class Random { public: typedef int RandomValue; Random& operator = (int seed) { X = seed; return *this; } Random(int seed = 1):X(seed){}; int operator()(int seed = 0) { const int MM = 2147483647; const int AA = 48271; const int QQ = 44488; const int RR = 3399; if (seed != 0) X = seed; X = AA*(X%QQ)-RR*(X/QQ); if (X < 0) X += MM; return X-1; } int operator()(int min, int max) { return (*this)()%(max-min) + min; } private: int X; }; class Random64 { typedef unsigned long long uint64; public: typedef uint64 RandomValue; Random64& operator = (uint64 seed) { X = seed; return *this; } Random64(uint64 seed = 0):X(seed){}; uint64 operator()(uint64 seed = uint64(-1)) { const uint64 a = 3202034522624059733ULL; const uint64 c = 1ULL; if (seed != uint64(-1)) X = seed; uint64 Y = a * X + c; X = a * Y + c; Y = (Y&0xFFFFFFFF00000000ULL) | (X >> 32); return Y; } uint64 operator()(uint64 min, uint64 max) { return (*this)()%(max-min) + min; } private: uint64 X; }; 

    To whom it will be necessary, here is the solution:

     main { uint32_t stat[1]; stat[0] = 2595719; uint32_t rang = 0; int min = 10; int max = 1000; for (int i = 0; i < 10000; i++) { rang = rand(stat) % (max - min) + min; cout << "Random Number is " << rang << endl; } } uint32_t rand(uint32_t state[1]) { uint32_t x = state[0]; x ^= x << 13; x ^= x >> 17; x ^= x << 5; state[0] = x; return x; } 

    Suggest to correct if there are options.

    • It will seem to give out a certain sequence of numbers each time the program is launched, no? - Andrej Levkovitch
    • @AndrejLevkovitch, rand also produces a specific sequence of numbers each time the program starts. Only because of the srand it will begin each time with a new number. - eanmos
    • @eanmos I’m talking about this - Andrej Levkovitch
    • @AndrejLevkovitch here, so in stat[0] I add the current time in seconds, it seems to be unique. - Fresto
    • one
      @AndrejLevkovitch a, I added after. For example, let it be. Surprisingly, I never knew what was inside the generator and how it was built, and everything is easy, it turns out - Fresto

    To write your PRNG you can use the linear congruential method . It is often used in the implementation of the standard library. This method can generate a statistically good pseudo-random sequence (i.e., the frequency of occurrence of each number will be approximately the same), but this method is not applicable in cryptography, because it can be easily cracked .

    A portable implementation of the rand and srand functions can be found in the C standard (an example is the implementation from C11):

     static unsigned long long int next = 1; int rand(void) { next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768; } void srand(unsigned int seed) { next = seed; } 

    ISO / IEC 9899: 2011


    Notes
    The next variable is declared static, because it is assumed that it and (both functions, moreover) is in the stdlib.h file.

    • It seems to me that it return (unsigned int)(next>>16)&0x7FFF; to write return (unsigned int)(next>>16)&0x7FFF; . Explicit optimization! - kisssko
    • @kisssko, I think the compiler will do everything himself. In any case, this is a quotation from the standard. - eanmos
    • possible and will do. That's why I pointed out - "explicit optimization". To not seem, but for sure. :) - kisssko
    • Then immediately, bringing the cat by the balls: return (unsigned int)next&0x7FFF; - bukkojot

    Why don't you use the standard C method:

     srand(time(NULL)); digit = begin_num + rand() % n; 
    • I write with the help of OpenCL, the most primitive things are built in there. I have to dodge as I can) - Fresto
    • Cannot add library. The author of the answer wrote his generator in these conditions: rand() - vp_arth
    • @HolyBlackCat as I understood from the description, it cannot use the generator from c ++, but not from C - Andrej Levkovitch
    • @Fresto can even do without rand() - but for this you will need to take time on your own (you can’t do without time) and divide it into a large number. Actually this is what srand and rand - Andrej Levkovitch
    • @AndrejLevkovitch, hmm, in principle, I can still pass the time. But with the rest of the calculation? Is there any primitive example for this? It is within two numbers. - Fresto

    I can offer one more simple example:

     class RandInt { using Cont = std::valarray<int>; Cont v; static int k; public: RandInt(int min, int max, size_t seed) { k = seed; int langht = max - min; v = Cont(min, langht); for (int i = 1; i < langht; ++i) v[i] = v[i -1] + 1; } int operator ()() { v = v.cshift(k); ++k; return v[v.size()/2]; } }; int RandInt::k; int main(]) { RandInt r(-25, 25, 3); for (int i = 0; i < 20; ++i) std::cout << r() << '\n'; return 0; } 
    • This example is in C ++, can you please do this implementation for C? I just indicated this in the question - Fresto