Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:26:06+00:00 2026-06-14T14:26:06+00:00

For testing and learning purpose, I wanted to modify the php rand and mt_rand

  • 0

For testing and learning purpose, I wanted to modify the php rand and mt_rand functions which are in https://github.com/php/php-src:ext/standard/rand.c.

I wanted to give a fixed output each time rand function is called and for this purpose I modified the code

PHPAPI long php_rand(TSRMLS_D)
{
    long ret;

    if (!BG(rand_is_seeded)) {
        php_srand(GENERATE_SEED() TSRMLS_CC);
    }

#ifdef ZTS
    ret = php_rand_r(&BG(rand_seed));
#else
# if defined(HAVE_RANDOM)
    ret = random();
# elif defined(HAVE_LRAND48)
    ret = lrand48();
# else
    ret = rand();
# endif
#endif

    // ignoring the results coming from the calls above and 
    // returning a constant value 
    ret = 3264;

    return ret;
}

compiled

./configure
make
make install

and finally called the rand function as echo rand(3000,4000); and it always returns 3000.

What would be the way to modify this function? and why there is TSRMLS_D but not the range parameters?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:26:07+00:00Added an answer on June 14, 2026 at 2:26 pm

    The actual function that is called from PHP is the one declared PHP_FUNCTION(rand), which calls the php_rand() function that you modified. It does the following:

    PHP_FUNCTION(rand)
    {
        long min;
        long max;
        long number;
        int  argc = ZEND_NUM_ARGS();
    
        if (argc != 0 && zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE)
            return;
    
        number = php_rand(TSRMLS_C);
        if (argc == 2) {
            RAND_RANGE(number, min, max, PHP_RAND_MAX);
        }
    
        RETURN_LONG(number);
    }
    

    As you can see, if you pass 2 parameters in, it calls the RAND_RANGE macro, which transforms the result of php_rand() into the range given.

    #define RAND_RANGE(__n, __min, __max, __tmax) \
        (__n) = (__min) + (long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
    

    If you want it always to output a given value, I would recommend modifying PHP_FUNCTION(rand), instead of php_rand().

    Though if all you’re looking for is stable random number outputs, so that you get the same result every time, if you don’t care about the exact result, you can just call srand(0) at the beginning of your program, to seed the random number generator with a constant value. This will mean that the random number generator will always give you the same sequence of random numbers, which is useful for testing purposes, and can be done without patching PHP itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Duplicate : https://stackoverflow.com/questions/135651/learning-unit-testing I'm trying to develop some software for my research group to
For local testing (and learning), I wish to use x.com host name instead of
I am learning PHP PEAR and testing the following code. However when I login,
I'm just learning unit testing. This php code class Foo { public function bar($arg)
I'm using a micro instance of ec2 for learning and testing purpose of my
I know this is fairly subjective, but I'm diving into testing and learning about
I'm learning and testing some graph libraries and am running into a weird problem(but
I'm learning how unit testing is done in Rails, and I've run into a
I am just learning how to do unit-testing. I'm on Python / nose /
I am learning about the Runtime class in java, and am testing the use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.