확률이 다른 임의의 정수

확률이 다른 임의의 정수

이는 후속 조치입니다.난수에 대한 내 마지막 게시물.

내 새로운 질문은 다음과 같습니다.

\n무작위로 (일종의) 생성하고 싶다고 가정해 보겠습니다 .

\n=1 10%의 시간

\n=2 20%의 시간

\n=3 30%의 시간

\n=4 40%의 시간

이 코드는 가능한 각 값을 \n동일하게 만듭니다. 위에 나열된 확률에 어떻게 가중치를 부여합니까?

\documentclass{minimal}

\setlength\parindent{0pt}

\usepackage{pgffor}

\begin{document}

\pgfmathdeclarerandomlist{choices}{{1}{2}{3}{4}}
\foreach\x in {1,...,50}
{\pgfmathrandomitem{\n}{choices}\n\\}

\end{document}

답변1

이전 질문에 대한 내 답변을 변형하여 3개 ifthenelse를 중첩할 수 있습니다.

\documentclass[border=4mm]{article}
\usepackage{pgffor}
\begin{document}
\foreach\x in {1,...,2000} {
 \pgfmathsetmacro{\tmp}{rnd}
 \pgfmathparse{ifthenelse(\tmp<=0.1,1,ifthenelse(\tmp<=0.3,2,ifthenelse(\tmp<=0.6,3,4)))}\pgfmathresult
}
\end{document}

egreg의 답변에서 약간 빌리십시오.

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\newcounter{1}\newcounter{2}\newcounter{3}\newcounter{4}
\foreach\x in {1,...,2000} {
 \pgfmathsetmacro{\tmp}{rnd}
 \pgfmathparse{ifthenelse(\tmp<=0.1,1,ifthenelse(\tmp<=0.3,2,ifthenelse(\tmp<=0.6,3,4)))}\pgfmathresult
 \stepcounter{\pgfmathresult}
}

1: \the\value{1}\par
2: \the\value{2}\par
3: \the\value{3}\par
4: \the\value{4}\par
\end{document}

여기에 이미지 설명을 입력하세요

답변2

\documentclass{article}
\usepackage{pgffor}

\newcommand{\myrandom}{%
  \expandafter\domyrandom\pdfuniformdeviate 10 \domyrandom
}
\def\domyrandom#1\domyrandom{%
  \ifcase#1
  1\or
  2\or
  2\or
  3\or
  3\or
  3\or
  4\or
  4\or
  4\or
  4\fi
}

\begin{document}

\foreach \x in {1,...,100}{\myrandom\space}

\newcounter{1}\newcounter{2}\newcounter{3}\newcounter{4}
\foreach \x in {1,...,1000}{\stepcounter{\myrandom}}
1: \the\value{1}\par
2: \the\value{2}\par
3: \the\value{3}\par
4: \the\value{4}\par

\end{document}

여기에 이미지 설명을 입력하세요

답변3

1부터 4까지의 정수를 생성하여 확률이 0.1, 0.2, 0.3, 0.4가 되도록 하는 직접적인 방법이 있다고 확신하지만,간접적인또는 2단계 방법이 설정이 더 간단합니다. 먼저 1에서 10 사이의 정수를 무작위로 생성합니다. (즉, 각 정수에는 가 있습니다 P=0.1.) 둘째, 정수가 각각 2, 4, 7보다 작은지 확인하고 그에 따라 숫자 "1", "2", "3"을 할당하고 숫자 "4"를 연결합니다. "를 "해당 사항 없음" 범주로 지정합니다. 즉, 정수가 7에서 10 사이인 경우입니다.

다음은 이 아이디어를 LuaLaTeX 기반으로 구현한 것입니다.

여기에 이미지 설명을 입력하세요

\documentclass{article}
\newcommand\x{%
  \directlua{x=math.random(10) % draw an integer between 1 and 10
             if       x<2 then tex.sprint(1) % true if x==1
               elseif x<4 then tex.sprint(2) % true if x==2 or 3
               elseif x<7 then tex.sprint(3) % true if x==4, 5, or 6
               else            tex.sprint(4) % true if x==7, 8, 9, or 10
             end}}
\begin{document}
\obeylines % just for this example
\x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x
\x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x
\x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x
\x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x
\end{document}

답변4

https://en.wikipedia.org/wiki/Pseudo-random_number_sampling

원하는 분포에서 원하는 정수 값을 출력하는 y 값으로 0에서 1까지의 x 값을 갖는 함수를 만듭니다. 그런 다음 난수 생성기를 사용하여 0과 1 사이의 숫자를 생성한 다음 함수 값을 찾습니다. 함수는 "x" 값에 100을 곱하고 내림하는 100개 요소 배열일 수 있습니다.

관련 정보