How to use rand() function to generate random numbers between 0 and 2^30-1 in Windows system

In Windows systems, the rand() function can generate random numbers in the range of 0~32767. I tried modifying the value of RAND_MAX, but it didn't work. The generated random numbers were still between 0 and 32767.

so what should I do now? ? ? ? ?

We can randomly generate a 32768 base number. Because 32768=2^15, in order to ensure that it does not exceed the range of int, this 32768 hexadecimal number cannot exceed 2 digits. Then, we can generate bit by bit.

Code:

int bigrand(){//生成0 ~ 2^30-1之间的随机数 
	int a = rand();//32768进制数的高位
	int b = rand();//32768进制数的低位
	return a * 32768 + b;//计算出这个数
}

Call the bigran() function to generate a random number between 0~2^31-1.

Guess you like

Origin blog.csdn.net/nnKevi/article/details/120612371