c++ 난수 발생 코치 좀 부탁드립니다...

ringgoo28의 이미지

제가 난수 발생 프로그램 짜고 있는데요.

문제가 1부터 10 사이의 난수를 발생하는 프로그램을 작성하는건데요. 조건이 Rnd 클래스를 정의하고 객체를 선언, Rnd 클래스는 난수를 저장하는 하나의 전용멤버 변수를 갖고, 난수를 생성하여 출력하는 create()함수를 공용 멤버로 정의하라.

인데요. 제가 짜봤는데 문제가 뭘까요? 그리고 rand()함수 쓰려면 헤더파일에 선언해야 되는거

아닌가요? 시드값 초기화하는 문장에서 time을 못 읽게다던데요. 문제가 뭘까요..

제가봐도 엉성하긴한데 어느부분부터 잡아야될지 모르겠네요.. 조금만이라도 코치 좀 부탁드립니다.

#include
#include
using namespace std;

class Rnd {
private :
int random;
public :
void set_create(int a){
random = rand();};
void get_create(){
return random;}

};

int main(void)
{
int x;

Rnd random;
create.set_create(x);
cout <<" 난수 발생 : " << x << create.set_create() << endl;

return 0;

}

nhamfnad의 이미지

#include iostream
#include cstdlib   // rand 함수는 표준 C 언어에 있으므로 다음과 같이 등록시킵니다.
using namespace std;
 
class Rnd {
private:
	double rn;    // 더블형으로 반환할 데이터 값을 만든후
        int n;        // 발생 범위설정
public:
	void set_value(int a) {
 
		srand(a);              // srand 함수를 이용해 seed 값을 초기화 합니다.
                                       // 만약 time 함수를 쓴다면 값 a 대신에 
                                       // timestamp 를 찍어서 unsigned int 형으로 초기화
                                       // 시키면 됩니다	             
                n = 10;
        }
	double get_value() {
 
                rn = rand()*(double(n + 1)/double(RAND_MAX + 1)) ; 
                                                 // 1 부터 n 까지 랜덤 함수 생성 
		return rn;      
	}	
};
int main(void) {
	unsigned int x, seed;
	seed = 1;
 
	Rnd obj; 
        obj.set_value(seed);
	for (x = 1; x <= 10;x++) {    // 루프를 10 번 돌리면 10개의 로또?숫자가 나옵니다	
		cout << "generated number is " << obj.get_value() << endl;
	}
	return 0;
}

아참, 위의 코드는 MinGW GCC(G++) 3.4 에서 잘 돌아갑니다.
제가 봐도 좀 어설프군요...

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.