LinuxForum.Fans
Тема

random в си.

Автор: gnomsi 5 сообщений 2423 просмотров
#164239

Люди и троли wink . Возникла проблема, нужен рандом! Адекватную инфу гугл не даёт :( . Нужна дока по нему.

#164242

man 3 rand
man 3 random
если нужен труъ рандом, то кроме чтения из /dev/random на ум ничего не приходит...

#165084

Решение найдено.Спасибо.

#165086
gnomsi написал:
Нужна дока по нему.

Д.Э.Кнут. второй том ИП.
там одна строчка всего...
ну и man 3 rand

text
EXAMPLE
       POSIX.1-2001 gives the following example of an implementation of rand() and srand(), possibly useful  when
       one needs the same sequence on two different machines.

static unsigned long next = 1;

/* RAND_MAX assumed to be 32767 */ int myrand(void) { next = next * 1103515245 + 12345; return((unsigned)(next/65536) % 32768); }

void mysrand(unsigned seed) { next = seed; }

text
       The  following  program  can be used to display the pseudo-random sequence produced by rand() when given a
       particular seed.

#include <stdlib.h> #include <stdio.h>

int main(int argc, char *argv[]) { int j, r, nloops; unsigned int seed;

if (argc != 3) { fprintf(stderr, "Usage: %s <seed> <nloops>\n", argv[0]); exit(EXIT_FAILURE); }

seed = atoi(argv[1]); nloops = atoi(argv[2]);

srand(seed); for (j = 0; j < nloops; j++) { r = rand(); printf("%d\n", r); }

exit(EXIT_SUCCESS); }

Войдите или зарегистрируйтесь, чтобы ответить.