小编rap*_*cil的帖子

Erlang多处理消息的接收和发送

我正在研究"学习一些Erlang"一书.我在使用Erlang进行多处理编程时有几个问题要实现.基本上,我正在阅读并模仿"了解你的一些二郎".我在程序中生成了一个带有一些导向函数的程序.它说冰箱可以像自己一样存放和拿走.所以我正在处理我的程序,以便在基本遵循以下的操作中执行某些操作:

  • 首先,我需要终止消息应该与store和take相同的形式.这意味着消息应该是{from, terminate},接收者回复消息{self(), terminated}.

  • 其次,我想通过消息回复发件人无组织{From, Msg}的消息{self(), {not_recognized, Msg}}.

  • 第三,添加库存消息{From, {inventory} }将返回到FoodListsender2进程中的当前发送者(From).

  • 第四,添加窥视消息{From, {peek, a} }将查看存储在该过程中的项目.如果存在时,将消息发送回发送器(从): {present, a}.如果不存在,将消息发送回发送器(从): {not_present, a}.

我对冰箱本身的使用感到困惑.我用教程中的示例代码想出了类似下面的内容:

-module(kitchen).
-compile(export_all).

fridge1() ->
receive
    {From, {store, _Food}} ->
        From ! {self(), ok},
        fridge1();
    {From, {take, _Food}} ->
        %% uh ...
        From ! {self(), not_found},
        fridge1();
    terminate             ->
            ok
end.

fridge2(FoodList) ->                        
receive
    {From, {store, Food}} ->
        From ! {self(), ok},
        fridge2( [Food | …
Run Code Online (Sandbox Code Playgroud)

erlang message multiprocessing

3
推荐指数
1
解决办法
175
查看次数

为什么在使用PRNG估算Pi时我总是得到2.8284?

我是C++的新手.我正在尝试使用计算机系统的随机数生成器基于Ernesto Cesaro的定理来统计确定Pi的值.但我现在所做的可以输入种子数并生成100个伪随机数,然后估计pi的值.生成器可以生成不同组的伪随机数.然而,令人困惑的是我总是得到2.8284的pi估计而没有变化.这是代码:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
   int seed;
   cout << "input a seed number: " << endl;
   cin >> seed;
   srand(seed);
   int i, a[100];
   for (i = 0; i < 100; i++)
      a[i] = rand() % 100 + 1;
      cout << "The generated random numbers are: " << endl;
   for (i = 0; i < 100; i++)
      cout << a[i] << "\t";
   int m, n, j, r;
   int  sum = 0;
   for (j = …
Run Code Online (Sandbox Code Playgroud)

c++ random estimation pi

0
推荐指数
1
解决办法
492
查看次数

标签 统计

c++ ×1

erlang ×1

estimation ×1

message ×1

multiprocessing ×1

pi ×1

random ×1