Thread: Random number in certain interval without using any standard function(algorithm)

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    3

    Random number in certain interval without using any standard function(algorithm)

    hello everyone, I want to share with you this simple and great idea to get a bounded random number algorithmically- follow this youtube video : [YouTube]

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    This took a very long time to explain
    r = min + rand() % (max - min);
    which skews the natural distribution of rand() - a standard function, by the way - to get results that you want.

    A better way would be to split RAND_MAX into groups equal to the size of your accepted range, and then divide into your result. This way you are at least working with rand()'s own distribution, and the results should be better because of it.

    Code:
    int random(int lim) {
       int value;
       int divisor = RAND_MAX / (1 + lim);
    
       do {
          value = rand() / divisor;
       } while (value > lim);
    
       return value;
    }
    
    int r = min + random(max - min);
    This does virtually the same thing as scaling a floating point number (i.e. ((double)lim) / RAND_MAX).
    Last edited by whiteflags; 09-30-2016 at 08:38 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by LabedNadjib82 View Post
    hello everyone, I want to share with you this simple and great idea to get a bounded random number algorithmically- follow this youtube video : [YouTube]
    The pattern you'll see in most professional programs is

    Code:
    /* get a uniformly distributed number p = 0  to 1 - episilon */
    #define uniform() (rand()/(RAND_MAX + 1.0))
    
    /* we can now use it to obtain integers in any bounds, like this */
    int randletter = 'a' + (int) (uniform() * 26);
    
    /* or if we need a lot of them, write a second macro */
    
    #define rnd(low, high)  ((int) (((high)-(low)+1)*uniform()) + (low))
    If you need better random numbers, you need to rewrite
    uniform() to use a better generator than rand().
    Often you want gaussian random numbers, however, with
    a bell-curve distribution.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number from interval [0,1]
    By nerio in forum C Programming
    Replies: 11
    Last Post: 02-07-2013, 07:24 AM
  2. Replies: 3
    Last Post: 03-16-2012, 04:42 AM
  3. C random number generator function...
    By towed in forum C Programming
    Replies: 6
    Last Post: 04-29-2011, 01:30 AM
  4. Repeat function with interval
    By Ideswa in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2006, 07:50 AM
  5. Replies: 2
    Last Post: 12-25-2003, 01:31 AM

Tags for this Thread