2017-02-15 44 views
0

我試圖找到隨機指數來選擇點雲中的某些點。 以下是代碼。但是,即使在使用srand()後,我所有的三次都得到相同的數字。有人可以幫忙,關於這個?點雲中的隨機指數

/* find three points randomly */ 
for (long i = 0; i < 3; ++i) 
{ 
    srand (time(NULL)); 
    cout <<"\nRandom index" << (rand() % points.size() + 1); 
} 
+0

循環大約[srand()函數(HTTP仔細閱讀:你會得到你的代碼有什麼不妥。 – Redanium

+1

是... $ srand()$必須被調用一次..在循環外...謝謝:) –

+0

不客氣;) – Redanium

回答

1

您在同一時間播種您的隨機生成器,每次循環迭代一次。

在開始相反種子一次:

/* find three points randomly */ 
srand(time(NULL)); 
for(int i = 0; i != 3; ++i) { 
    cout <<"\nRandom index" << (rand() % points.size() + 1); 
} 

你也不需要使用long爲三個步驟:)