rand() 에서 매번 똑같은 수가...

익명 사용자의 이미지

안녕하세요? ^^

rand() 함수에서 매번 똑같은 수가 나옵니다.

srand() 는 뭐죠? 이것을 해주지 않으면 항상 1이 나온다고 하던데...

( 리눅스 한글화 해서 man 보면 한글로 나오나요? --; )

실행할때마다 똑같이 나오지 않게 하는 방법이 뭐죠? ^^;;

답변 부탁드립니다~ )

익명 사용자의 이미지

rand() 는 수학적연산에 의해서 임의의 수를 구하기 때문에..
시작을 결정하는 seed 값이 같으면 항상 같은 값이 나오죠..
srand 는 seed 값을 결정해주는 함수구요..
(아마 microtime 이 기준이구요..)

그게 맘에 안드시면 /dev/random /dev/urandom 을 쓰셔도 되긴합니다..
(man urandom)

익명 사용자의 이미지

안녕하세요? ^^;

man urandom

을 치면... urandom 에 대해서 찾을수 없다고 나옵니다.

이럴때는 무엇을 설치해야 하죠? ^^;

그리고... srand() 에는 그럼 어떤값을 넣어주어야 하죠?

답변 부탁드립니다~! )

iron wrote..
rand() 는 수학적연산에 의해서 임의의 수를 구하기 때문에..
시작을 결정하는 seed 값이 같으면 항상 같은 값이 나오죠..
srand 는 seed 값을 결정해주는 함수구요..
(아마 microtime 이 기준이구요..)

그게 맘에 안드시면 /dev/random /dev/urandom 을 쓰셔도 되긴합니다..
(man urandom)

익명 사용자의 이미지

urandom 은 커널레벨에서 지원하는것입니다..
커널 옵션에 따라서 동작하지 않을수 있습니다..
urandom manpage 는 데비안에서는 manpages 패키지에 있는데요...
다른 배포본은 잘 모르겠네요..
혹시 배포본이 오래된게 아닌지요 -.-;;

srand 는 보통 srand(time(NULL)) 이렇게 많이 쓰는 것 같습니다만 -.-a

익명 사용자의 이미지

man 4 urandom 해보시고,...

그냥 rand나 random함수를 쓰실거면,
seed로 잘 겹치지않는수 (gettimofday로 읽은
tv_usec등이 좋겠죠?)를 쓰시면 별 문제 없을겁니다.

struct timeval seed;
gettimeofday( &seed, NULL );
srandom( seed.tv_usec );
cout << random() << endl;

boolean의 이미지

shuffle 알고리즘을 만들어 돌려주세요

익명 사용자의 이미지

날짜를 잘 보세요. 18년 전에 답글이 끊겼습니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.