c++ - 将区间[0,1]分成(3j-2)个子区间

标签 c++ c algorithm random

此代码生成 50 个 [0,1] 范围内的随机数。

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] segmentation 为 (3j-2) 个大小为 [0,a1],[a1,a1+a2],......,[ai ,1] (i=1...3j-3).

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

最佳答案

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

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

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

随机生成一个 x,y,每个都在 0.0 到 1.0 范围内

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

此时再 segmentation 这个盒子,从而再制作 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;
}

关于c++ - 将区间[0,1]分成(3j-2)个子区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17002258/

相关文章:

c++ - cmake:如何使用静态链接的 boost (或嵌入 boost )创建库

c++ - 在 C++ 中存储货币值的最佳方式

c++ - std::quick_exit 和 std::abort 有什么区别,为什么需要 std::quick_exit?

c - 如何在 linux 上将两个虚拟地址映射到同一物理内存上?

c++ - 如何对指针使用统一初始化?

c - C结构的内存布局可视化

c - Vim quickfix 模式与 Oracle Pro*C 文件?

algorithm - 我的 padovan 系列代码有什么问题?

java - 黑客排名 : Sherlock and Anagrams

c# - 使用 DateTime.Now.Ticks 生成唯一的数字 ID