小编ddn*_*mad的帖子

如何在Ruby中获得具有给定离散分布的随机数

I'm coding my university assignment that is somewhat connected with distributions and random roll stuff. So the question is: how to get a random number with a given discrete distribution in Ruby.

To be more specific: in trivial example with normal discrete distribution like (0 with P=1/2; 1000 with P=1/2) I could write such a function:

 def chooseNumber(a,b)
  rval = Random.rand(0..1)
  return a if rval == 0
  return b
 end 
Run Code Online (Sandbox Code Playgroud)

First question: is there any way to write it using …

ruby random probability

5
推荐指数
1
解决办法
269
查看次数

PHP 的 error_get_last() 函数的 Python 替代品是什么?

我需要实现一个atexitPython 函数来获取最后一个错误对象并检查它的类型。如果 python 错误的类型对应于 PHP,E_ERROR我应该将错误的输出保存到文件中。

我正在移植的 PHP 代码如下所示:

register_shutdown_function( "fatal_handler" );
function fatal_handler() {
  $error = error_get_last();
    if ($error != null && $error['type'] === E_ERROR)
        echo  "recordFatalError: {$error['message']}\n";
}
Run Code Online (Sandbox Code Playgroud)

我的代码快照如下:

def fatal_handler():
   # How to get last error object?

atexit.register(fatal_handler)
Run Code Online (Sandbox Code Playgroud)

如果有人向我解释如何使用 python 获得必要的功能,我会很高兴。

php python porting exception

5
推荐指数
1
解决办法
128
查看次数

是否可以在“if”中使用“retry”关键字?

我必须用begin- rescue-end块包围所有东西。我写的代码看起来像:

begin
  bet = Dialogs.enter_your_bet
  approx.calculate_average_profit(bet)
  approx.print_profits_table
  retry if Dialogs.play_again?
rescue;retry
end
Run Code Online (Sandbox Code Playgroud)

该行retry if Dialogs.play_again?导致以下错误:

./main:14: Invalid retry
./main: compile error (SyntaxError)
Run Code Online (Sandbox Code Playgroud)

是否可以在没有常规多行方法的情况下使这种内联与retry子句一起if使用?ifend

ruby if-statement inline

2
推荐指数
1
解决办法
1913
查看次数

标签 统计

ruby ×2

exception ×1

if-statement ×1

inline ×1

php ×1

porting ×1

probability ×1

python ×1

random ×1