그래서 숫자 1 - 6의 바이어스를 포함하는 < int, float>지도가 있습니다. 편향된 값을 반환해야합니다. 바이어스는 사용자가 입력 할 때 일정하지 않습니다. 클래스에 있으므로 모든 변수가 이미 선언되어 있습니다.바이어스가있는 난수
그래서 지금까지 내가 한 것입니다.
int die:: randomNumber(map < int, float > &biasPercent)
{
randomString[0] << biasPercent[0];
randomArray[0] = randomString[0].str();
maximum = randomArray[0].size();
for(counter = 0; counter < 6; counter++)
{
randomString[counter] << biasPercent[counter];
randomArray[counter] = randomString[counter].str();
if(randomArray[counter].size() > maximum)
{
maximum = randomArray[counter].size();
}
}
limit = 1;
for(counter = 1; counter <= maximum - 2; counter++)
{
limit *= 10;
}
srand(time(NULL));
random1 = rand() % limit + 1;
if(random1 <= biasPercent[0] * limit)
{
return 1;
}
else if(random1 > biasPercent[0] * limit && random1 <= (biasPercent[0] * limit) + (biasPercent[1] * limit))
{
return 2;
}
else if(random1 > ((biasPercent[0] * limit) + (biasPercent[1] * limit) * limit) && random1 <= (((biasPercent[0] * limit) + (biasPercent[1] * limit)) + (biasPercent[2] * limit)))
{
return 3;
}
else if(random1 > (((biasPercent[0] * limit) + (biasPercent[1] * limit)) + (biasPercent[2] * limit)) && random1 <= (((((biasPercent[0] * limit) + (biasPercent[1] * limit)) + (biasPercent[2] * limit))) + (biasPercent[3] * limit)))
{
return 4;
}
else if(random1 > (((((biasPercent[0] * limit) + (biasPercent[1] * limit)) + (biasPercent[2] * limit))) + (biasPercent[3] * limit)) && random1 <= ((((((biasPercent[0] * limit) + (biasPercent[1] * limit)) + (biasPercent[2] * limit))) + (biasPercent[3] * limit))) + ((biasPercent[4] * limit)))
{
return 5;
}
else
{
return 6;
}
}
항상 1을 반환하기 때문에 작동하지 않는 것처럼 보입니다. 나는 stringstream이 그것과 관련이 있다고 생각합니다. 그러나 float을 문자열로 변환 한 다음 가장 많은 문자로 값을 가져 오는 것보다 난수의 한도를 아는 방법을 알지 못합니다. 도와주세요.
C++에는 불균일 한 분포를 명시 적으로 지원하는 더 나은 무작위 라이브러리' '이 있습니다. 이제는 자신 만의 배포판을 작성해야하는데, 이는 배포본을 다루기 만하면됩니다. 난수 생성 부분이 아니라 배포 부분 만 다루면됩니다. –
MSalters
모든 숫자를 생성하기 전에'srand'를 호출하는 것이 도움이되지 않습니다. 'rand '를 고수하면 프로그램 시작시'srand'를 한 번 호출하십시오. –