LINUX.ORG.RU

а в чём проблема-то?

jtootf ★★★★★
()

/* Один раз при запуске программы. */
srand((unsigned int) time(NULL));
...
/* Получаем случайное число от 0 до RAND_MAX включительно. */
int x = rand();

Deleted
()
Ответ на: комментарий от Deleted

Это был ответ на сообщение.

> Записать вывод Int ret = system(); в переменную С.


А это я вообще не понял.

Deleted
()
Ответ на: комментарий от Deleted

mironov_ivan, значение RAND_MAX указывать как именованную константу? Что-то не катит: 

#include <stdio.h>

#define RAND_MAX 8
main()
{
srand((unsigned int) time(NULL));
int a = rand();
printf("%d\n", a);
}


выводит больший диапазон значений.                                 

ShTH
() автор топика
Ответ на: комментарий от ShTH

int a = rand()/RAND_MAX*(YOUR_MAXIMUM-YOUR_MINUMUM)+YOUR_MINIMUM;

совсем никак?

накрайняк если не заботит, нормальное распределение для случайной величины

rand()%YOUR_MAXIMUM;

qnikst ★★★★★
()
Ответ на: комментарий от ShTH

> mironov_ivan, значение RAND_MAX указывать как именованную константу? Что-то не катит:

Во-первых, это не именованная константа. И во-вторых, это значение не изменить. RAND_MAX определён в stdlib.h, как и функции rand и srand.

Deleted
()
Ответ на: комментарий от qnikst

> int a = rand()/RAND_MAX*(YOUR_MAXIMUM-YOUR_MINUMUM)+YOUR_MINIMUM;

rand()/RAND_MAX - без приведения к типу с плавающей точкой тут почти всегда получится ноль.

Deleted
()
Ответ на: комментарий от Deleted

А как тогда полчить значение допустим между 0 и 15? Предыдущие примеры не компилятся - может какая ещё стандартная библиотека кроме stdio.h нужна?

ShTH
() автор топика
Ответ на: комментарий от ShTH

> А как тогда полчить значение допустим между 0 и 15?

http://www.azillionmonkeys.com/qed/random.html

> Предыдущие примеры не компилятся - может какая ещё стандартная библиотека кроме stdio.h нужна?


Это не библиотека, а заголовочный файл. Тебе нужно включить stdlib.h, о котором я написал выше. А библиотека тут участвует всего одна - libc, она подключается автоматически и специально компоновщику её указывать не нужно.

Deleted
()
Ответ на: комментарий от Deleted

точно, но это уже 2ой уровень получения того, что надо :)

qnikst ★★★★★
()
Ответ на: комментарий от Deleted

>Но лучше всё-таки так не делать.

это самое простое решение; все остальные - сложнее и лучше

jtootf ★★★★★
()

C FAQ -> http://c-faq.com/lib/index.html

13.16


Q: How can I get random integers in a certain range?

A: The obvious way,

	rand() % N		/* POOR */

(which tries to return numbers from 0 to N-1) is poor, because the low-order bits of many random number generators are distressingly non-random. (See question 13.18.) A better method is something like

	(int)((double)rand() / ((double)RAND_MAX + 1) * N)

If you'd rather not use floating point, another method is

	rand() / (RAND_MAX / N + 1)

If you just need to do something with probability 1/N, you could use

	if(rand() < (RAND_MAX+1u) / N)

All these methods obviously require knowing RAND_MAX (which ANSI #defines in <stdlib.h>), and assume that N is much less than RAND_MAX.

When N is close to RAND_MAX, and if the range of the random number generator is not a multiple of N (i.e. if (RAND_MAX+1) % N != 0), all of these methods break down: some outputs occur more often than others. (Using floating point does not help; the problem is that rand returns RAND_MAX+1 distinct values, which cannot always be evenly divvied up into N buckets.) If this is a problem, about the only thing you can do is to call rand multiple times, discarding certain values:

	unsigned int x = (RAND_MAX + 1u) / N;
	unsigned int y = x * N;
	unsigned int r;
	do {
		r = rand();
	} while(r >= y);
	return r / x;

For any of these techniques, it's straightforward to shift the range, if necessary; numbers in the range [M, N] could be generated with something like

	M + rand() / (RAND_MAX / (N - M + 1) + 1)

(Note, by the way, that RAND_MAX is a constant telling you what the fixed range of the C library rand function is. You cannot set RAND_MAX to some other value, and there is no way of requesting that rand return numbers in some other range.)

If you're starting with a random number generator which returns floating-point values between 0 and 1 (such as the last version of PMrand alluded to in question 13.15, or drand48 in question 13.21), all you have to do to get integers from 0 to N-1 is multiply the output of that generator by N:

	(int)(drand48() * N)

Additional links

References: K&R2 Sec. 7.8.7 p. 168
PCS Sec. 11 p. 172 

quaternion
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.