Smallest possible value of lcg_value()?

Discussion in 'PHP' started by offthedome, Sep 29, 2011.

  1. #1
    I need to know what the smallest possible value of lcg_value() is, for the purpose of creating a small chance of getting a free homework for my students. I tried googling it but didn't find it. Does anyone know what that smallest value is?
     
    offthedome, Sep 29, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    What does the function??? lcg_value() return? It's hard to give an answer without knowing what lcg_value() does.
     
    jestep, Sep 29, 2011 IP
  3. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #3
    It returns a pseudorandom number between 0 and 1, noninclusive.
     
    offthedome, Sep 29, 2011 IP
  4. fortehlolz

    fortehlolz Banned

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Without completely understanding, my answer is use PHP min() ?
     
    fortehlolz, Sep 30, 2011 IP
  5. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #5
    min() returns the smallest element of a list. I need the smallest possible value returned by the php random number generating function, lcg_value().

    I thought people would know what lcg_value() is. For reference: http://www.w3schools.com/php/func_math_lcg_value.asp
     
    offthedome, Sep 30, 2011 IP
  6. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #6
    I don't know why the thought of just generating a bunch of random numbers to simply read it off the page didn't occur to me. The answer is approximately 14 digits of precision, with some longer ones going on probably due to the way the script is run. Here's a small sample of what the function produces:

    0.27234924349065
    0.10337326026089
    0.4000373424102
    0.58617706192217
    0.15285599275822
    0.24723714146366
    0.40839973273908
    0.978778374051
    0.55012398455304
    0.044163945403432
    0.26011281750403
    0.3706700623484
    0.59564308536454
    0.40859574634406
    0.25052007784304
    0.34125788927765
    0.52033644097704
    0.94514817386963
     
    offthedome, Sep 30, 2011 IP
  7. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #7
    For future reference, if you want to learn the mechanics behind a function, there's no better place to go than the source.

    ext/standard/lcg.c:
    /*
       +----------------------------------------------------------------------+
       | PHP Version 5                                                        |
       +----------------------------------------------------------------------+
       | Copyright (c) 1997-2011 The PHP Group                                |
       +----------------------------------------------------------------------+
       | This source file is subject to version 3.01 of the PHP license,      |
       | that is bundled with this package in the file LICENSE, and is        |
       | available through the world-wide-web at the following url:           |
       | http://www.php.net/license/3_01.txt                                  |
       | If you did not receive a copy of the PHP license and are unable to   |
       | obtain it through the world-wide-web, please send a note to          |
       | license@php.net so we can mail you a copy immediately.               |
       +----------------------------------------------------------------------+
       | Author: Sascha Schumann <sascha@schumann.cx>                         |
       +----------------------------------------------------------------------+
    */
    
    /* $Id: lcg.c 306939 2011-01-01 02:19:59Z felipe $ */
    
    #include "php.h"
    #include "php_lcg.h"
    
    #if HAVE_UNISTD_H
    #include <unistd.h>
    #endif
    
    #ifdef PHP_WIN32
    #include "win32/time.h"
    #else
    #include <sys/time.h>
    #endif
    
    #ifdef ZTS
    int lcg_globals_id;
    #else
    static php_lcg_globals lcg_globals;
    #endif
    
    #ifdef PHP_WIN32
    #include <process.h>
    #endif
    
    /*
     * combinedLCG() returns a pseudo random number in the range of (0, 1).
     * The function combines two CGs with periods of
     * 2^31 - 85 and 2^31 - 249. The period of this function
     * is equal to the product of both primes.
     */
    
    #define MODMULT(a, b, c, m, s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m
    
    static void lcg_seed(TSRMLS_D);
    
    PHPAPI double php_combined_lcg(TSRMLS_D) /* {{{ */
    {
        php_int32 q;
        php_int32 z;
    
        if (!LCG(seeded)) {
            lcg_seed(TSRMLS_C);
        }
    
        MODMULT(53668, 40014, 12211, 2147483563L, LCG(s1));
        MODMULT(52774, 40692, 3791, 2147483399L, LCG(s2));
    
        z = LCG(s1) - LCG(s2);
        if (z < 1) {
            z += 2147483562;
        }
    
        return z * 4.656613e-10;
    }
    /* }}} */
    
    static void lcg_seed(TSRMLS_D) /* {{{ */
    {
        struct timeval tv;
    
        if (gettimeofday(&tv, NULL) == 0) {
            LCG(s1) = tv.tv_sec ^ (tv.tv_usec<<11);
        } else {
            LCG(s1) = 1;
        }
    #ifdef ZTS
        LCG(s2) = (long) tsrm_thread_id();
    #else
        LCG(s2) = (long) getpid();
    #endif
    
        /* Add entropy to s2 by calling gettimeofday() again */
        if (gettimeofday(&tv, NULL) == 0) {
            LCG(s2) ^= (tv.tv_usec<<11);
        }
    
        LCG(seeded) = 1;
    }
    /* }}} */
    
    static void lcg_init_globals(php_lcg_globals *lcg_globals_p TSRMLS_DC) /* {{{ */
    {
        LCG(seeded) = 0;
    }
    /* }}} */
    
    PHP_MINIT_FUNCTION(lcg) /* {{{ */
    {
    #ifdef ZTS
        ts_allocate_id(&lcg_globals_id, sizeof(php_lcg_globals), (ts_allocate_ctor) lcg_init_globals, NULL);
    #else
        lcg_init_globals(&lcg_globals);
    #endif
        return SUCCESS;
    }
    /* }}} */
    
    /* {{{ proto float lcg_value()
       Returns a value from the combined linear congruential generator */
    PHP_FUNCTION(lcg_value)
    {
        RETURN_DOUBLE(php_combined_lcg(TSRMLS_C));
    }
    /* }}} */
    
    /*
     * Local variables:
     * tab-width: 4
     * c-basic-offset: 4
     * End:
     * vim600: sw=4 ts=4 fdm=marker
     * vim<600: sw=4 ts=4
     */
    
    
    Code (markup):
     
    Alex Roxon, Sep 30, 2011 IP