iOS 生成随机数

下文注意开闭区间就行

1.  获取 [1,5]之间的随机数

     int x = arc4random() % 5 + 1;

2. 获取 [0,100)之间的随机数

     int x = arc4random() % 100;

3. 获取 [50,100]之间的随机数

    int x = arc4random() % (100 - 50 + 1) + 50;

4. 获取 [num1,num2]之间的随机数

   - (NSInteger)getRandomNumber:(NSInteger)num1 to:(NSInteger)num2
  {
       NSInteger num = arc4random()%(num2 - num1 + 1) + num1;
       return num;
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++语言实现的代码: ```c++ #include <iostream> #include <fstream> #include <string> #include <ctime> #include <cstdlib> #include <algorithm> using namespace std; struct Student { int id; string name; int score[6]; }; void generateScore(Student& s) { for(int i = 0; i < 6; i++) { s.score[i] = rand() % 101; } } void generateData(int num, ofstream& binOut, ofstream& txtOut) { for(int i = 1; i <= num; i++) { Student s; s.id = i; s.name = "S" + to_string(i); generateScore(s); binOut.write((char*)&s, sizeof(Student)); txtOut << s.id << " " << s.name << " "; for(int j = 0; j < 6; j++) { txtOut << s.score[j] << " "; } txtOut << endl; } } bool cmp(Student s1, Student s2) { return s1.id < s2.id; } void sortFile(ifstream& in, ofstream& out) { in.seekg(0, ios::end); int size = in.tellg(); in.seekg(0, ios::beg); Student* arr = new Student[size / sizeof(Student)]; in.read((char*)arr, size); sort(arr, arr + size / sizeof(Student), cmp); out.write((char*)arr, size); delete[] arr; } int main() { srand(time(NULL)); const int num = 50; ofstream binOut1("in1.bin", ios::binary); ofstream txtOut1("in1.txt"); ofstream binOut2("in2.bin", ios::binary); ofstream txtOut2("in2.txt"); ofstream binOut3("in3.bin", ios::binary); ofstream txtOut3("in3.txt"); ofstream binOut4("in4.bin", ios::binary); ofstream txtOut4("in4.txt"); ofstream binOut5("in5.bin", ios::binary); ofstream txtOut5("in5.txt"); generateData(num, binOut1, txtOut1); generateData(num, binOut2, txtOut2); generateData(num, binOut3, txtOut3); generateData(num, binOut4, txtOut4); generateData(num, binOut5, txtOut5); binOut1.close(); txtOut1.close(); binOut2.close(); txtOut2.close(); binOut3.close(); txtOut3.close(); binOut4.close(); txtOut4.close(); binOut5.close(); txtOut5.close(); ifstream in1("in1.bin", ios::binary); ofstream out1("in1_sorted.bin", ios::binary); ifstream in2("in2.bin", ios::binary); ofstream out2("in2_sorted.bin", ios::binary); ifstream in3("in3.bin", ios::binary); ofstream out3("in3_sorted.bin", ios::binary); ifstream in4("in4.bin", ios::binary); ofstream out4("in4_sorted.bin", ios::binary); ifstream in5("in5.bin", ios::binary); ofstream out5("in5_sorted.bin", ios::binary); sortFile(in1, out1); sortFile(in2, out2); sortFile(in3, out3); sortFile(in4, out4); sortFile(in5, out5); in1.close(); out1.close(); in2.close(); out2.close(); in3.close(); out3.close(); in4.close(); out4.close(); in5.close(); out5.close(); ifstream txtIn1("in1.txt"); ofstream txtOut1Sorted("in1_sorted.txt"); ifstream txtIn2("in2.txt"); ofstream txtOut2Sorted("in2_sorted.txt"); ifstream txtIn3("in3.txt"); ofstream txtOut3Sorted("in3_sorted.txt"); ifstream txtIn4("in4.txt"); ofstream txtOut4Sorted("in4_sorted.txt"); ifstream txtIn5("in5.txt"); ofstream txtOut5Sorted("in5_sorted.txt"); string line; getline(txtIn1, line); txtOut1Sorted << line << endl; getline(txtIn2, line); txtOut2Sorted << line << endl; getline(txtIn3, line); txtOut3Sorted << line << endl; getline(txtIn4, line); txtOut4Sorted << line << endl; getline(txtIn5, line); txtOut5Sorted << line << endl; txtIn1.close(); txtIn2.close(); txtIn3.close(); txtIn4.close(); txtIn5.close(); ifstream txtIn1Sorted("in1_sorted.txt"); ofstream txtOut1Sorted2("in1_sorted2.txt"); ifstream txtIn2Sorted("in2_sorted.txt"); ofstream txtOut2Sorted2("in2_sorted2.txt"); ifstream txtIn3Sorted("in3_sorted.txt"); ofstream txtOut3Sorted2("in3_sorted2.txt"); ifstream txtIn4Sorted("in4_sorted.txt"); ofstream txtOut4Sorted2("in4_sorted2.txt"); ifstream txtIn5Sorted("in5_sorted.txt"); ofstream txtOut5Sorted2("in5_sorted2.txt"); while(getline(txtIn1Sorted, line)) { txtOut1Sorted2 << line << endl; } while(getline(txtIn2Sorted, line)) { txtOut2Sorted2 << line << endl; } while(getline(txtIn3Sorted, line)) { txtOut3Sorted2 << line << endl; } while(getline(txtIn4Sorted, line)) { txtOut4Sorted2 << line << endl; } while(getline(txtIn5Sorted, line)) { txtOut5Sorted2 << line << endl; } txtIn1Sorted.close(); txtOut1Sorted2.close(); txtIn2Sorted.close(); txtOut2Sorted2.close(); txtIn3Sorted.close(); txtOut3Sorted2.close(); txtIn4Sorted.close(); txtOut4Sorted2.close(); txtIn5Sorted.close(); txtOut5Sorted2.close(); return 0; } ``` 该程序生成了5个输入文件,每个文件包含50个学生的成绩记录,以二进制格式和文本格式存储。然后,对每个文件进行排序,最终输出排序后的结果,也以二进制格式和文本格式存储。注意,程序中的排序算法使用了标准库函数`sort`,排序的依据是学生的学号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值