rand

rand -- generate a random value

Description

int rand(void );

Returns a pseudo-random value between 0 and RAND_MAX. If you want a random number between 5 and 15, for example, use rand()%10 + 5.

Remember to seed the random number generator before use with srand().

Here are two handy functions that use rand(). One returns a random integer less than a specified maximum value, the other does the same but returns a real value.

Example 1. rand example functions

function randomint($max = 100) { 
    static $startseed = 0; 
    if (!$startseed) {
        $startseed = (double)microtime()*getrandmax(); 
        srand($startseed);
    } 
    return (rand()%$max); 
} 

function random($max = 1) { 
    static $startseed = 0; 
    if (!$startseed) { 
        $startseed = (double)microtime()*getrandmax();
        srand($startseed); 
    }
    return ((rand()/getrandmax())*$max); 
}
	

See also srand() and getrandmax().




月光软件程序下载编程文档电脑教程网站设计网址导航网络文学游戏天地幽默笑话生活休闲写作范文安妮宝贝
电脑技术编程开发网络专区谈天说地情感世界游戏元素分类游戏热门游戏体育运动手机专区业余爱好影视沙龙
音乐天地数码广场教育园地科学大观古今纵横谈股论金人文艺术医学保健动漫图酷二手专区地方风情各行各业

月光软件站·版权所有