生成随机数 - srand c++

发布于 2024-12-08 13:49:58 字数 435 浏览 0 评论 0原文

我在使用 srand 时遇到问题。

我正在尝试生成一个 100 到 200 之间的随机数。

该数字将不断生成并放置在一个数组中。一旦再次调用该方法,就需要再次生成相同的随机数序列。

因此,我需要一颗种子,无论我如何尝试,我似乎都无法让它发挥作用。

我并不是在寻找任何人编写一些代码,而是只是向我展示生成此类数字的正确格式。

更新

我有一个火车对象,其中包含一个链接列表(内联列表中的每个位置都是一个车厢)。

每列火车的车厢数量需要在 100, 200 区间内随机。

每节车厢的煤炭量需要在 1000, 2000 区间内随机。

我正在尝试实现一个模拟器类,它将创建一列火车包含随机数据量的随机数量的车厢。

希望这更有意义。

正在为如何实施而苦恼。

I am having trouble using srand.

i am trying to generate a random number in the interval 100 to 200.

The number will keep being generated and placed in an array. Once the method is called again the same sequence of random numbers needs to be generated again.

Because of this I need a seed, no matter what I try I cannot seem to get it to work.

I am not looking for anyone to write some code rather just show me the correct formatting for generating such numbers.

UPDATE

I have a train object, which contains a linkedlist(each position in the linedlist is a carriage).

The number of carriages in each train needs to be random in the interval 100, 200.

The amount of coal in each carriage needs to be random in the interval 1000, 2000.

I am trying to implement a simulator class that will create a train with a random amount of carriages which contain a random amount of data.

Hope that makes a bit more sense.

Struggling on how to implement it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残月升风 2024-12-15 13:49:58

如果您只想重复任意序列,可以使用 srand() 为其提供相同的参数来设置种子。

例如:

pax$ cat qq.c
#include <iostream>
#include <cstdlib>

int main (void) {
    srand (42);
    for (int i = 0; i < 5; i++) {
        int x = 100 + (rand() % 101);
        std::cout << x << std::endl;
    }
    std::cout << "=====" << std::endl;
    srand (42);
    for (int i = 0; i < 5; i++) {
        int x = 100 + (rand() % 101);
        std::cout << x << std::endl;
    }
    return 0;
}

pax$ g++ -o qq qq.cpp ; ./qq
163
166
148
137
149
=====
163
166
148
137
149

If you just want to repeat an arbitrary sequence, you can set the seed with srand() by giving it the same argument.

For example:

pax$ cat qq.c
#include <iostream>
#include <cstdlib>

int main (void) {
    srand (42);
    for (int i = 0; i < 5; i++) {
        int x = 100 + (rand() % 101);
        std::cout << x << std::endl;
    }
    std::cout << "=====" << std::endl;
    srand (42);
    for (int i = 0; i < 5; i++) {
        int x = 100 + (rand() % 101);
        std::cout << x << std::endl;
    }
    return 0;
}

pax$ g++ -o qq qq.cpp ; ./qq
163
166
148
137
149
=====
163
166
148
137
149
雨后咖啡店 2024-12-15 13:49:58

尝试一下

void srand ( unsigned int seed );

如果种子设置为 1,则生成器将重新初始化为其初始值,并生成与调用 rand 或 srand 之前相同的值。

try this

void srand ( unsigned int seed );

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

笑梦风尘 2024-12-15 13:49:58

如前所述,您可以通过使用同一种子多次播种来进行粉碎。

srand(1234); // magic number

// .... 

srand(1234); // magic number again

rand() 的输出将从与之前相同的点重新开始。

当您这样做时,我建议不要在 rand() 上使用模运算符,因为它不会导致值的均匀分布。相反,您可以使用以下帮助程序来获取整数范围内的随机值:

int randRange(int M, int N)
{
     // see http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
     return M + rand() / ( RAND_MAX / ( N - M ) + 1 );
}

int nextrand = randRange(100,200);

请参阅链接的有关 Eternally Confuzzled 的文章(作者:Julienne Walker)了解更多背景信息,以及播种

C++ 选项

上面的缺点是生成完全是顺序的(不能同时拥有随机生成器的两个实例)。既然您使用 C++ 进行标记,我们为什么不使用它呢!

您可以使用 tr1 或 c++0x/c++111 uniform_int_distribution

#include <random>
#include <functional>

std::uniform_int_distribution<int> distribution(100, 200);
std::mt19937 engine; // Mersenne twister MT19937

int nextrand  = distribution(engine);

直接的好处是您可以让多个引擎同时生成相同的序列(请参阅示例)。

正如您所看到的,您可以像 engine(1234) 这样的生成器进行播种,就像使用 srand 一样。
查看实时示例:

1 Boost.Random(如果使用 C++03)

As mentioned, you can srand by seeding it more than once with the same seed.

srand(1234); // magic number

// .... 

srand(1234); // magic number again

The output of rand() will be restarted from the same point as before.

While you're at it, I recommend against using the modulo operator on rand(), because it will not result in a uniform distribution of value. Instead, you can use the following helper to get a random value in an integer range:

int randRange(int M, int N)
{
     // see http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
     return M + rand() / ( RAND_MAX / ( N - M ) + 1 );
}

int nextrand = randRange(100,200);

See the linked article on Eternally Confuzzled (by Julienne Walker) for more background, also on seeding.

C++ options

The above has the drawback that the generation is entirely sequential (you can not have two instances of the random generator at the same time). Since you are tagging with C++, why don't we use it!

You can use tr1 or c++0x/c++111 uniform_int_distribution:

#include <random>
#include <functional>

std::uniform_int_distribution<int> distribution(100, 200);
std::mt19937 engine; // Mersenne twister MT19937

int nextrand  = distribution(engine);

An immediate advantage is that you can have multiple engines simultaneously generating the same sequence (see sample).

As you can also see, you can seed the generator like engine(1234) just like with srand.
See the sample live on:

  • C++03 with boost: https://ideone.com/FC4xm

    #include <boost/random.hpp>
    #include <boost/random/uniform_int.hpp>
    
    int main()
    {
        boost::mt19937 engine1(1234);
        boost::mt19937 engine2(1234);
        boost::uniform_int<> dist(100,200);
    
        for (int i=0; i<20; i++)
        {
             std::cout << dist(engine1) << " is equal to " << dist(engine2) << std::endl;
        }
    }
    

  • C++0x http://ideone.com/467Aj
    Also demonstrating a little syntactic sugar:

    auto generator = std::bind(distribution, engine);
    nextrand = generator(); // more convenient use
    

1 Boost.Random if you use C++03

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文