通过数组随机递增,C ++

时间:2019-02-28 21:17:47

标签: c++ arrays indexing srand

我正在努力理解使用'rand()'从整数数组中随机读取数字的概念。我创建了一个介于1-3之间的随机数生成器,并希望输出一个数组的索引,然后让生成器从前一个索引中随机输出下一个生成的数字,直到它到达数组的末尾。例如:

  1. 'rand()'= 3,'数组[2]'

  2. 'rand()'= 2,'array [4]'

  3. 'rand()'= 3,'array [7]'

如果说得通??等等

我当前使用的代码只是输出一个随机数序列。我放置了一个“种子”,以便可以查看相同的顺序。

int main() 
{ 
 int arrayTest[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
 17, 18, 19, 20};   
 srand(4);
 for(int i = 0; i < 20; i++)  
    {  
     arrayTest[i] = (rand() % 3);
     cout << arrayTest[i] << endl;
    }




}

1 个答案:

答案 0 :(得分:1)

我在猜测您真正想要的是什么。但它似乎想要对索引进行随机递增,并使用该索引循环读取数组中的数据。

因此,此代码无法执行您想要的任何操作

    VGG(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace)
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace)
    (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace)
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace)
    (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (13): ReLU(inplace)
    (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (15): ReLU(inplace)
    (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (18): ReLU(inplace)
    (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): ReLU(inplace)
    (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (22): ReLU(inplace)
    (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace)
    (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU(inplace)
    (2): Dropout(p=0.5)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace)
    (5): Dropout(p=0.5)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
)

它使用顺序(即非随机)索引将随机值写入(而不是读取)数组。

这就是我你想要的

 arrayTest[i] = (rand() % 3);

但是我不确定,尤其是int main() { int arrayTest[20] = { ... }; srand(4); int index = -1; for(int i = 0; i < 20; i++) { index += (rand() % 3) + 1; // add random number from 1 to 3 to index if (index >= 20) // if index too big for array index -= 20; // wrap around to beginning of array cout << arrayTest[index] << endl; // read array at random index, and output } } 的数字从1到20的顺序让我有些怀疑。也许如果您解释了为什么想要做任何您想做的事情,那会更加清楚。