-1
if (ii == 3){//车辆只停了一个小时,有3/4的概率离场
    srand((unsigned)time(NULL)*100);
    int a = 1+rand() % 4;
    int b = 1+rand() % 4;
    int c = 1+rand() % 4;
    cout << "++++3 " << a << "++++" << endl;//用作测试判断是否离场函数
    cout << "++++3 " << b << "++++" << endl;
    cout << "++++3 " << c << "++++" << endl;
    cout << "*************" << endl;
    if ((a == 1 && b == 2 && c == 3)||(a==2&&b==1&&c==3)||(a==3&&b==2&&c==1)||(a==3&&b==2&&c==1))
        return true;
    else
        return false;
}

Just like this picture, the program gives me the same number ,but I need different numbers , how can I do it.

#ifndef Head_H
#define Head_H
#include<stdlib.h>
#include<iostream>
#include<time.h>
#define PathSize 5
#define ParkSize 10
#define Price 5
using namespace std;
srand((unsigned)time(NULL));
MD XF
  • 7,860
  • 7
  • 40
  • 71

1 Answers1

1

Use srand once, at the start of your program.

If you repeatedly re-seed the generator then you ruin its statistical properties.

Moving on:

  1. Avoid using % to rescale the generator. This will introduce statistical bias (unless by pure luck the modulo is a multiple of RAND_MAX).

  2. Look at the C++11 random number library. This contains better-specified generators than plain old rand, which is awful on some platforms.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483