开发者

repeated random numbers in a loop [duplicate]

开发者 https://www.devze.com 2023-03-13 08:46 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Repeated random number in a loop
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Repeated random number in a loop

thanks for the comments before, i updated the code.

First in the main function i call the srand

int main ()
{
//DefineRelationstozero (); //All predecessors are set to -1
srand((unsigned)time(0));
.......
......
......
for (int k=0;k<iterationnumber;k++)
{
.....
RandomKey ();//i call randomkey for every iteration
}...

void RandomKey ()
{


        for (int k=0;k<ActivityNumber;k++)
         {

        Act_num[k].Priority=(rand()%10000)*0.0001;//random number

         }


        for (int i=0;i<ActivityNumber;i++)
           arr[i]=Act_num[i].Priority;               

....
开发者_运维知识库

RESULT: for every iteration i got exactly same numbers in arr[]


maybe Act_num[k].Priority is int. When you call:

(rand()%10000)*0.0001

it gives you number from <0, 0.999>. So, if Act_num[k].Priority is integer then you get floor from this random number. So it's 0 for all time

0

精彩评论

暂无评论...
验证码 换一张
取 消