2월, 2021의 게시물 표시

C: rand() 와 srand() 로 난수 만들기

C 언어에서 난수를 만들 때 가장 널리 쓰이는 함수는 rand() 와 srand() 이다. 이 글에서는 이 두 함수의 사용법과 활용법을 알아보고 한계와 대안에 대해서 다루려고 한다.   1. rand() rand() 함수의 원형은 다음과 같다. #include <stdlib.h> int rand(void); rand() 함수는 [0, RAND_MAX] 범위(0과 RAND_MAX 포함)에서 정수 형태의 의사-난수(pseudo-random number)를 만든다. RAND_MAX 는 rand() 가 만드는 난수의 최대값으로, C 라이브러리에 따라 다르다. 다만, 최소 크기는 0x7FFF(=32767, 15비트)로 보장된다.   1-1. 원하는 범위내의 난수 만들기 rand() 함수는 [0, RAND_MAX] 범위의 난수를 만든다. 하지만, 실제로 필요한 범위는 이와는 다르다. 예를 들어, [10, 20] 사이의 난수를 만들려면 어떻게 해야 할까?  보통 두 가지 방법이 알려져 있다. 하나는 나머지 연산(%) 을 이용하는 것이고, 다른 하나는 비례 관계 를 이용하는 것이다.   1-1-1. 나머지 연산을 이용하는 방법 나머지 연산을 이용하는 방법부터 알아보자. 나머지 연산을 이용하면 [0, n) 범위(0 포함, n 비포함)를 얻을 수 있다. 예를 들어 n=7 이라면, 어떤 값이 오더라도 7로 나눈 나머지는 결국 0 이상 6 이하의 정수이다. 여기에 적당한 값을 더해 주면 우리가 원하는 범위의 난수를 얻을 수 있다. 이를 함수로 나타내면 다음과 같다. Colored By Color Scripter ™ 1 2 3 4 int  randrange( int  a,  int  b) {      return  (rand() % (b - a + 1)) + a; } 하지만, 이 방법의 경우 난수가 고르게 만들어지지 않는다는 문제점이 있다. 예를 들어, 난수가 만들어지는 범위는 [0, 8] 이지만, 원하는 범위는 [0, 5] 라면, 나머지 연산에는 6 이 쓰인다

OS/2 codes: How to get size of free physical memory

OS/2 provides APIs to get size of being interested in system memory. For example, DosQuerySysInfo() with QSV_TOTPHYSMEM for total size of physical memory, QSV_TOTRESMEM for total size of system-resident memory, and QSV_TOTAVAILMEM for total size of total available memory for all processes. However, those indice do not tell us about free physical memory. Actually, any 32-bits APIs do not provide such information at all. Instead, you can use 16-bits APIs. That is, DosMemAvail() . Here is the prototype of DosMemAvail(): APIRET16 APIENTRY16 Dos16MemAvail( PULONG pulAvailMem ) For details, see https://komh.github.io/os2books/prcp/080_L2_DosMemAvail.html . VisualAge C++ and Watcom can use this prototype directly. However, unfortunately, gcc does not provide features to call 16-bits functions directly. Instead, you should use thunking. Fortunately, kLIBC provides macros for thunking. You can call DosMemAvail() on gcc/kLIBC like this: Colored By Color Scripter ™ 1 2 3 4 5 6 7 8 9 USHORT _T

OS/2 announce: libebml v1.4.1 and libmatroska v1.6.2 for OS/2 released

 libebml v1.4.1 and libmatroska v1.6.2 for OS/2 has been released. OS/2 용 libebml v1.4.1 와 libmatroska v1.6.2 를 발표하였습니다. They can be downloaded from : 다음 링크를 클릭하면 받을 수 있습니다 : libebml v1.4.1: http://www.os2.kr/komh/os2factory/#libebml libmatroska v1.6.2: http://www.os2.kr/komh/os2factory/#libmatroska Enjoy Warping !!! Korean OS/2 User Community : http://www.os2.kr/

OS/2 announce: git v2.30.0 for OS/2 released

git v2.30.0 for OS/2 has been released. OS/2 용 git v2.30.0 을 발표하였습니다. They can be downloaded from : 다음 링크를 클릭하면 받을 수 있습니다 : http://www.os2.kr/komh/os2factory/#git Enjoy Warping !!! Korean OS/2 User Community : http://www.os2.kr/  

OS/2 announce: fluidsynth v2.1.7 for OS/2 released

fluidsynth v2.1.7 for OS/2 has been released. OS/2 용 fluidsynth v2.1.7 을 발표하였습니다. They can be downloaded from : 다음 링크를 클릭하면 받을 수 있습니다 : http://www.os2.kr/komh/os2factory/#fluidsynth Enjoy Warping !!! Korean OS/2 User Community : http://www.os2.kr/