将间隔[0,1]除以(3j-2)子区间

时间:2013-06-08 17:54:34

标签: c++ c algorithm random

此代码在范围[0,1)中生成50个随机数。

int main(){

int i,j,M=50;

// If the interval is not uniform
double interval_widths[3] = { 0.1, 0.11, 0.03};

double interval_widths_sum[3];


//Divide into subintervals
void Init() {
  interval_widths_sum[0] = 0;
  for (int i=1; i<N; i++) {
    interval_widths_sum[i] = interval_widths_sum[i-1] + interval_widths[i];
  }
}

//check in which interval R is
int Seek(double R) {
  int i;
  if (R < 0.0) return -1;
  for (i = 0; i < N; i++) {
    if (R >= interval_widths_sum[i]) {
        break;
    }
   return (i);
  }

}

  unsigned long init[4] = {0x123, 0x234, 0x345, 0x456}, length = 4;

  MTRand drand; 

  for (i = 0; i < M; i++) {

    double x=("%10.8f ", drand());
    double y=("%10.8f ", drand());
    double R=("%10.8f ", drand());
    cout<<"(x,y)="<< x <<","<< y<< endl;
    a[i]=x*y;
       double *p= &x;
    double *q= &y;
    double *r= &R;
    double z= Seek(*r);
    cout<<"(x,y)="<<*p<<","<<*q<<endl;
    cout<< Init<<" "<< z<<endl;
  }
}

1)在[0,1]范围内生成x和y的值。可以说x = 0.11,0.23,..... 和y = 0.13,0.33,.....等;

2)现在定义a = x * y。如a1 = 0.11 * 0.13,a2 = 0.23 * 0.33等。

3)现在我想将区间[0,1]细分为大小[0,a1],[a1,a1 + a2],......,[ai]的(3j-2)子区间,1](i = 1 ... 3j-3)。

4)然后生成范围[0,1]内的数字R.并检查(3j-2)中哪一个包含此R.

1 个答案:

答案 0 :(得分:2)

[编辑对OP的目标的错误理解。希望现在更接近。]

您有N个间隔(框),其中N=(3*j-2)

当j = 1时,你有一个方框(0,0)和(1,1)

随机生成x,y,每个都在0.0到1.0

的范围内

鉴于此x,y找到N个框中的哪一个包含此点。查看每个现有的框,看看(x,y)是否在范围内。存在更快的方法,但这是一个起点。

此时对此框进行细分,从而再制作3个框。

根据需要重复

#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>

double drand() {
  return rand()/(1.0 + RAND_MAX);
}


typedef struct Box_s {
  double x0, y0, x1, y1;
} Box_t;

#define Box_N (303)
int Box_Count = 0;
Box_t Box[Box_N];

void Box_Init() {
  memset(Box, 0, sizeof(Box));
  Box[0].x0 = 0.0;
  Box[0].y0 = 0.0;
  Box[0].x1 = 1.0;
  Box[0].y1 = 1.0;
  Box_Count = 1;
}

int Box_Find(double x, double y) {
  for (int b=0; b<Box_Count; b++) {
    if ((Box[b].x0 <= x) && (x <= Box[b].x1) && (Box[b].y0 <= y) && (y <= Box[b].y1)) {
      return b;
    }
  }
  printf("Box not found %e %e\n", x, y);
  exit(1);
}

void Box_Divide(int b, double x, double y) {
  if ((Box[b].x0 <= x) && (x <= Box[b].x1) && (Box[b].y0 <= y) && (y <= Box[b].y1)) {
    // Make 3 more boxes
    if ((Box_Count + 3) >= Box_N) {
      printf("Not enough boxes\n");
      exit(1);
    }
    Box[Box_Count].x0 = x;
    Box[Box_Count].x1 = Box[b].x1;
    Box[Box_Count].y0 = Box[b].y0;
    Box[Box_Count].y1 = y;
    Box_Count++;
    Box[Box_Count].x0 = x;
    Box[Box_Count].x1 = Box[b].x1;
    Box[Box_Count].y0 = y;
    Box[Box_Count].y1 = Box[b].y1;
    Box_Count++;
    Box[Box_Count].x0 = Box[b].x0;
    Box[Box_Count].x1 = x;
    Box[Box_Count].y0 = y;
    Box[Box_Count].y1 = Box[b].y1;
    Box_Count++;
    // Update original box
    Box[b].x1 = x;
    Box[b].y1 = y;
    return;
  }
  printf("x y not in box %d %e %e\n", b, x, y);
  exit(1);
}


void Box_Print() {
  double TotalArea = 0.0;
  printf("\n");
  printf("%3s (%5s, %5s) ( %5s, %5s) %5s\n", "#", "x0", "y0", "x1", "y1", "Area");
  for (int b=0; b<Box_Count; b++) {
    double Area = (Box[b].x0 - Box[b].x1) * (Box[b].y0 - Box[b].y1);
    printf("%3d (%5.3f, %5.3f) ( %5.3f, %5.3f) %5.3f\n", b, Box[b].x0, Box[b].x1, Box[b].y0, Box[b].y1, Area);
    TotalArea += Area;
  }
  printf("%3d  %5s  %5s    %5s  %5s  %5.3f\n", Box_Count, "", "", "", "", TotalArea);
}

int main(int argc, char *argv[]) {
  Box_Init();
  for (int rcount = 0; rcount < 50; rcount++) {
    Box_Print();
    double x = drand();
    double y = drand();
    int b = Box_Find(x, y);
    Box_Divide(b, x, y);
  }
  Box_Print();
  return 0;
}