1

此代码在 [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] 细分为 (3j-2) 大小为 [0,a1],[a1,a1+a2],......,[ai,1] 的子区间(i=1...3j-3)。

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

4

1 回答 1

2

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

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

当 j=1 时,您有 1 个角在 (0,0) 和 (1,1) 的盒子

随机生成 ax,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;
}
于 2013-06-08T18:39:01.643 回答