Search code examples
cfunctionrandomcomplex-numbersfractals

Complex random point generation


Using C, how can I generate a random point on the complex plain?


Solution

  • As a decent starting point, you could use rand(). To generate a random number between -2 and +2, you can do:

    float f = 4 * ((rand() / (float)RAND_MAX) - 0.5f);
    

    If you repeat this process, you can get an x and a y:

    float x = 4 * ((rand() / (float)RAND_MAX) - 0.5f);
    float y = 4 * ((rand() / (float)RAND_MAX) - 0.5f);
    

    Now just use x and y as your complex number.

    Also, be sure to call srand() once (and only once) at the start of your program to seed the random number generator.