Hàm random trong khoảng giới hạn trong C++

xin chào mọi người.
Mọi người có thể cho em hỏi có hàm nào lấy một số ngẫu nhiên trong 1 khoảng không. Ví dụ như em muốn lấy 1 số ngẫu nhiên từ 1 đến 13 thì dùng hàm ra sao. Em tìm trên mạng thì có mỗi hàm rand(), nhưng nó không có giới hạn theo mình yêu cầu.
Mong mọi người có thể giúp đỡ cho em

Kết hợp phép chia lấy dự
Random 1 số trong khoảng a đến b

int n = rand() % (b - a + 1) + a;

Có thể sử dụng thêm hàm srand() trong thư viện ctime để random theo thời gian. Chứ k thì hàm rand() tắt đi random lại vẫn số cũ

1 Like

rand() % 13 + 1

2 Likes

viết 1 cái class luôn:

#include <iostream>
#include <chrono>
#include <random>

class Random
{
public:
    template<typename T>
    static typename std::enable_if<std::is_integral<T>::value, T>::type range(T low, T high)
    {
        std::uniform_int_distribution<T> dist(low, high);
        return dist(prng);
    }
    template<typename T>
    static typename std::enable_if<std::is_floating_point<T>::value, T>::type range(T low, T high)
    {
        std::uniform_real_distribution<T> dist(low, high);
        return dist(prng);
    }
private:
    static std::mt19937 prng;
};

std::mt19937 Random::prng{std::chrono::high_resolution_clock::now().time_since_epoch().count()};


int main()
{
    for (int i = 0; i < 10; ++i)
        std::cout << Random::range(1, 13) << " ";
    std::cout << "\n";
    for (int i = 0; i < 10; ++i)
        std::cout << Random::range(1.0, 13.0) << " ";
    std::cout << "\n";
    for (int i = 0; i < 10; ++i)
        std::cout << Random::range(1.0f, 13.0f) << " ";
    std::cout << "\n";
}

dài dòng thế chứ xài thì cứ Random::range(a, b) là ngon lành, a, b cùng kiểu, là số nguyên hay số thực gì cũng chơi tuốt :joy:

4 Likes

C++11 :v mà có hỗ trợ hàm RNG tự viết không nhỉ.

2 Likes

hàm rng tự viết là sao? Là phân phối (distribution) tự chọn đó hả? https://en.wikipedia.org/wiki/Inverse_transform_sampling Vọc thử nè: https://tritran.xyz/posts/random-voi-pdf-bat-ki/

còn thay cái prng mt19937 thì ko biết :joy: Muốn viết thử thì xem http://www.pcg-random.org/ coi người ta viết thế nào, mà phải pass đủ loại test xác suất lằng nhằng lắm :joy:

2 Likes

Không :slight_smile: cài cái này này :smiley: http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf

Paper của pcg thì nên đọc từ trang 25 trở đi, mà có vẻ phải đọc từ từ.

2 Likes

http://en.cppreference.com/w/cpp/concept/UniformRandomBitGenerator

hình như thỏa 4 điều kiện là được rồi nè: có result_type, có min(), có max() và operator() sinh số ngẫu nhiên.

edit:

#include <iostream>
#include <cstdint>
#include <random>

class MyHorribleRng
{
public:
    using result_type = uint32_t;
    MyHorribleRng(result_type seed=1) : seed{seed} {}
    result_type min()const { return 0; }
    result_type max()const { return 4294967295; }
    result_type operator()() { return seed = seed * 3; }
private:
    result_type seed;
};

int main()
{
    MyHorribleRng rng;
    std::uniform_int_distribution<> dist(0, 9);
    for (int i = 0; i < 100; ++i)
        std::cout << dist(rng) << " ";
    std::cout << "\n";
}

được nè :joy:

3 Likes
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
	srand(time(0));
	cout<<1+rand()%13<<" ";
return 0;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?