C++伪随机数

直接上代码吧

用的是vs2019

#include <iostream>
using namespace std;



int main() {
	// 系统生成随机数  // rand()%100 生成 0~99
	srand(time(NULL));  // 随机数种子,不加这行下一行就是伪随机数
	int random_num = rand() % 100 + 1;  // 1-100; 
	
	//cout << random_num << endl;
	
	

	while (true)
	{
		int guess;
		cout << "输出你猜的数:";
		cin >> guess;

		if (random_num == guess)
		{
			cout << "恭喜你!猜对了" << endl;
			break;
		}
		else if (random_num > guess)
		{
			cout << "猜小了!" << endl;
		}
		else
		{
			cout << "猜大了!" << endl;
		}
	}


	return 0;
}
posted @ 2023-04-09 11:07  __username  阅读(25)  评论(0)    收藏  举报