Sunday, May 06, 2007
Perl 펄] 32비트 16진수 헥사(Hex) 난수; 32bit 2진수 Random(랜덤) 발생 함수
C언어도 그렇지만, 펄에서도 32비트 랜덤 정수를 제대로 만들지 못했습니다.
int(rand(0xFFFFFFFF));
이런 코드로는 안되더군요. 그래서 32bit 랜덤 정수가 제대로 나오는 함수를 만들었습니다. 우선 0과 1만의 난수를 구한 후, 그 난수를 32비트 정수에 비트 단위로 집어넣어 주었습니다.
소스 파일명: example.pl
※ 아래 박스 클릭 후, 키보드 화살표 키로 좌우 스크롤 가능함
실행 결과 화면:
※ 아래 박스 클릭 후, 키보드 화살표 키로 좌우 스크롤 가능함
▶▶ [Perl/펄] 랜덤(난수;Random)/무작위한 숫자 출력 함수 - rand - srand
int(rand(0xFFFFFFFF));
이런 코드로는 안되더군요. 그래서 32bit 랜덤 정수가 제대로 나오는 함수를 만들었습니다. 우선 0과 1만의 난수를 구한 후, 그 난수를 32비트 정수에 비트 단위로 집어넣어 주었습니다.
십육진수/이진수 랜덤 생성: 0x00000000 ~ 0xFFFFFFFF
소스 파일명: example.pl
※ 아래 박스 클릭 후, 키보드 화살표 키로 좌우 스크롤 가능함
#!/usr/bin/perl
use strict; use warnings;
# 32비트 랜덤 정수 만들어 출력
print "16진수 2진수 난수 unsigned 10진수 (signed 10진수)\n";
print "===========================================================================\n";
for (my $i = 1; $i <= 50; $i++) {
my $r32 = random32();
printf("%08X %032b %10u (%11d)\n", $r32, $r32, $r32, $r32);
}
# 32bits Random int 1개를 반환하는 함수
sub random32 {
my $result = 0;
for (my $i = 31; $i >= 0; $i--) {
$result = setAbit( $result, $i, int(rand(2)) );
}
return $result;
}
# $x의 $n번째 비트를 $b의 값으로 설정하는 함수
sub setAbit {
my $x = shift;
my $n = shift;
my $b = shift;
return $x | (1 << $n) if ($b == 1);
return $x & (~(1 << $n));
}
use strict; use warnings;
# 32비트 랜덤 정수 만들어 출력
print "16진수 2진수 난수 unsigned 10진수 (signed 10진수)\n";
print "===========================================================================\n";
for (my $i = 1; $i <= 50; $i++) {
my $r32 = random32();
printf("%08X %032b %10u (%11d)\n", $r32, $r32, $r32, $r32);
}
# 32bits Random int 1개를 반환하는 함수
sub random32 {
my $result = 0;
for (my $i = 31; $i >= 0; $i--) {
$result = setAbit( $result, $i, int(rand(2)) );
}
return $result;
}
# $x의 $n번째 비트를 $b의 값으로 설정하는 함수
sub setAbit {
my $x = shift;
my $n = shift;
my $b = shift;
return $x | (1 << $n) if ($b == 1);
return $x & (~(1 << $n));
}
실행 결과 화면:
※ 아래 박스 클릭 후, 키보드 화살표 키로 좌우 스크롤 가능함
D:\Z>example.pl
16진수 2진수 난수 unsigned 10진수 (signed 10진수)
===========================================================================
6A84F5BF 01101010100001001111010110111111 1787098559 ( 1787098559)
BF4C0F6E 10111111010011000000111101101110 3209432942 (-1085534354)
694F7ED6 01101001010011110111111011010110 1766817494 ( 1766817494)
F6F15123 11110110111100010101000100100011 4143010083 ( -151957213)
C46DC04A 11000100011011011100000001001010 3295526986 ( -999440310)
7DAAE109 01111101101010101110000100001001 2108350729 ( 2108350729)
30C115F6 00110000110000010001010111110110 817960438 ( 817960438)
8A7968A8 10001010011110010110100010101000 2323212456 (-1971754840)
7480EE47 01110100100000001110111001000111 1954606663 ( 1954606663)
2AE0C447 00101010111000001100010001000111 719373383 ( 719373383)
5CAE2174 01011100101011100010000101110100 1554915700 ( 1554915700)
5CF6AA08 01011100111101101010101000001000 1559669256 ( 1559669256)
0F7E7F3C 00001111011111100111111100111100 259948348 ( 259948348)
2B63502B 00101011011000110101000000101011 727928875 ( 727928875)
6B4A8BF9 01101011010010101000101111111001 1800047609 ( 1800047609)
991B2E05 10011001000110110010111000000101 2568695301 (-1726271995)
8BDB3303 10001011110110110011001100000011 2346398467 (-1948568829)
F84F087D 11111000010011110000100001111101 4165929085 ( -129038211)
D487FD41 11010100100001111111110101000001 3565681985 ( -729285311)
54FFCD29 01010100111111111100110100101001 1426050345 ( 1426050345)
7A519650 01111010010100011001011001010000 2052167248 ( 2052167248)
BD5A9E8D 10111101010110101001111010001101 3176832653 (-1118134643)
48CDD04C 01001000110011011101000001001100 1221447756 ( 1221447756)
6342C7C0 01100011010000101100011111000000 1665320896 ( 1665320896)
265A0674 00100110010110100000011001110100 643434100 ( 643434100)
8FDB9895 10001111110110111001100010010101 2413533333 (-1881433963)
82A0B4E4 10000010101000001011010011100100 2191570148 (-2103397148)
62C640C3 01100010110001100100000011000011 1657159875 ( 1657159875)
C7689625 11000111011010001001011000100101 3345520165 ( -949447131)
3AFF5485 00111010111111110101010010000101 989811845 ( 989811845)
3C70B773 00111100011100001011011101110011 1014019955 ( 1014019955)
DE2E3D36 11011110001011100011110100110110 3727572278 ( -567395018)
8C6935FE 10001100011010010011010111111110 2355705342 (-1939261954)
4FF436F7 01001111111101000011011011110111 1341404919 ( 1341404919)
D8251933 11011000001001010001100100110011 3626309939 ( -668657357)
822BB050 10000010001010111011000001010000 2183901264 (-2111066032)
4A84C270 01001010100001001100001001110000 1250214512 ( 1250214512)
93E78E69 10010011111001111000111001101001 2481426025 (-1813541271)
CB64F496 11001011011001001111010010010110 3412391062 ( -882576234)
1613F5E3 00010110000100111111010111100011 370406883 ( 370406883)
4680D2D5 01000110100000001101001011010101 1182847701 ( 1182847701)
2C6DA9C1 00101100011011011010100111000001 745384385 ( 745384385)
5D9FF0FC 01011101100111111111000011111100 1570763004 ( 1570763004)
4E8EF54D 01001110100011101111010101001101 1317991757 ( 1317991757)
42011144 01000010000000010001000101000100 1107366212 ( 1107366212)
AB27D0E1 10101011001001111101000011100001 2871513313 (-1423453983)
532C88C7 01010011001011001000100011000111 1395427527 ( 1395427527)
229D2B06 00100010100111010010101100000110 580725510 ( 580725510)
6BAAF513 01101011101010101111010100010011 1806365971 ( 1806365971)
5B8F636A 01011011100011110110001101101010 1536123754 ( 1536123754)
D:\Z>
16진수 2진수 난수 unsigned 10진수 (signed 10진수)
===========================================================================
6A84F5BF 01101010100001001111010110111111 1787098559 ( 1787098559)
BF4C0F6E 10111111010011000000111101101110 3209432942 (-1085534354)
694F7ED6 01101001010011110111111011010110 1766817494 ( 1766817494)
F6F15123 11110110111100010101000100100011 4143010083 ( -151957213)
C46DC04A 11000100011011011100000001001010 3295526986 ( -999440310)
7DAAE109 01111101101010101110000100001001 2108350729 ( 2108350729)
30C115F6 00110000110000010001010111110110 817960438 ( 817960438)
8A7968A8 10001010011110010110100010101000 2323212456 (-1971754840)
7480EE47 01110100100000001110111001000111 1954606663 ( 1954606663)
2AE0C447 00101010111000001100010001000111 719373383 ( 719373383)
5CAE2174 01011100101011100010000101110100 1554915700 ( 1554915700)
5CF6AA08 01011100111101101010101000001000 1559669256 ( 1559669256)
0F7E7F3C 00001111011111100111111100111100 259948348 ( 259948348)
2B63502B 00101011011000110101000000101011 727928875 ( 727928875)
6B4A8BF9 01101011010010101000101111111001 1800047609 ( 1800047609)
991B2E05 10011001000110110010111000000101 2568695301 (-1726271995)
8BDB3303 10001011110110110011001100000011 2346398467 (-1948568829)
F84F087D 11111000010011110000100001111101 4165929085 ( -129038211)
D487FD41 11010100100001111111110101000001 3565681985 ( -729285311)
54FFCD29 01010100111111111100110100101001 1426050345 ( 1426050345)
7A519650 01111010010100011001011001010000 2052167248 ( 2052167248)
BD5A9E8D 10111101010110101001111010001101 3176832653 (-1118134643)
48CDD04C 01001000110011011101000001001100 1221447756 ( 1221447756)
6342C7C0 01100011010000101100011111000000 1665320896 ( 1665320896)
265A0674 00100110010110100000011001110100 643434100 ( 643434100)
8FDB9895 10001111110110111001100010010101 2413533333 (-1881433963)
82A0B4E4 10000010101000001011010011100100 2191570148 (-2103397148)
62C640C3 01100010110001100100000011000011 1657159875 ( 1657159875)
C7689625 11000111011010001001011000100101 3345520165 ( -949447131)
3AFF5485 00111010111111110101010010000101 989811845 ( 989811845)
3C70B773 00111100011100001011011101110011 1014019955 ( 1014019955)
DE2E3D36 11011110001011100011110100110110 3727572278 ( -567395018)
8C6935FE 10001100011010010011010111111110 2355705342 (-1939261954)
4FF436F7 01001111111101000011011011110111 1341404919 ( 1341404919)
D8251933 11011000001001010001100100110011 3626309939 ( -668657357)
822BB050 10000010001010111011000001010000 2183901264 (-2111066032)
4A84C270 01001010100001001100001001110000 1250214512 ( 1250214512)
93E78E69 10010011111001111000111001101001 2481426025 (-1813541271)
CB64F496 11001011011001001111010010010110 3412391062 ( -882576234)
1613F5E3 00010110000100111111010111100011 370406883 ( 370406883)
4680D2D5 01000110100000001101001011010101 1182847701 ( 1182847701)
2C6DA9C1 00101100011011011010100111000001 745384385 ( 745384385)
5D9FF0FC 01011101100111111111000011111100 1570763004 ( 1570763004)
4E8EF54D 01001110100011101111010101001101 1317991757 ( 1317991757)
42011144 01000010000000010001000101000100 1107366212 ( 1107366212)
AB27D0E1 10101011001001111101000011100001 2871513313 (-1423453983)
532C88C7 01010011001011001000100011000111 1395427527 ( 1395427527)
229D2B06 00100010100111010010101100000110 580725510 ( 580725510)
6BAAF513 01101011101010101111010100010011 1806365971 ( 1806365971)
5B8F636A 01011011100011110110001101101010 1536123754 ( 1536123754)
D:\Z>
▶▶ [Perl/펄] 랜덤(난수;Random)/무작위한 숫자 출력 함수 - rand - srand
tag: perl
Perl | 펄
<< Home