Máy phát ngẫu nhiên đồ họa


10

Có một trình tạo ngẫu nhiên cho linux với GUI đẹp, có thể tạo một số nguyên ngẫu nhiên giữa hai số nguyên và hiển thị nó với một phông chữ lớn?


Bạn sẽ làm gì? Toán tâm thần?
BigSack

Câu trả lời:


36

Tôi không biết phần mềm nào. Google cũng không nghĩ ra thứ gì. Đoán đây là một vấn đề quá đơn giản. Nó sẽ có khoảng 30 dòng mã nếu bạn viết nó bằng ngôn ngữ scripting. Bạn cũng có thể tạo một bảng tính LibreScript để làm điều đó. Không nên quá khó khăn.

Chỉnh sửa 1:

trình tạo số giả ngẫu nhiên - tập lệnh perl gui

Dưới đây là một kịch bản perl nhanh chóng và bẩn mà tôi đã mã hóa. Bạn sẽ có thể tự sửa đổi nó. Khi bạn chạy nó với perl nameOfTheScript.plhoặc làm cho nó có thể thực thi được chmod u+x nameOfTheScript.plvà sau đó nhấp đúp vào nó, nó sẽ trông giống như trong hình trên.

#!/usr/bin/perl
# © 2011 con-f-use@gmx.net. Use permitted under MIT license: http://www.opensource.org/licenses/mit-license.php
use Gtk2 '-init'; # relies on the gnome toolkit bindings for perl

$size = 1e5;   # fontsize in 0.001 pt (only god knows why)

sub randomizeLabel {   #### this does the actual randomisation
    $min = int($entry1->get_text);
    $max = int($entry2->get_text);
    $rand = int(rand($max-$min+1)) + $min;
    $diplabel->set_markup( "<span size=\"$size\">$rand</span>" );
}
#### the rest is gui stuff:
$window = Gtk2::Window->new('toplevel');
$window->set_title('Random Integer Generator');
$window->signal_connect(destroy => sub { Gtk2->main_quit; });
$window->signal_connect(delete_event => sub { Gtk2->main_quit; });
$window->set_border_width(10);
$vbox = Gtk2::VBox->new(0, 5);   $window->add($vbox); $vbox->show;

$diplabel = Gtk2::Label->new;
$diplabel->set_markup("<span size=\"$size\">0</span>");
$vbox->add($diplabel);          $diplabel->show;

$entry1 = Gtk2::Entry->new;     $vbox->add($entry1);    $entry1->show;
$entry2 = Gtk2::Entry->new;     $vbox->add($entry2);    $entry2->show;

$button = Gtk2::Button->new("Generate!");
$button->signal_connect(clicked => \&randomizeLabel, $window);
$vbox->add($button);            $button->show;

$window->show;    Gtk2->main;
exit 0;

Chỉnh sửa2

ObsessiveFOSS yêu cầu tôi đưa vào một trình tạo khác cho các số ngẫu nhiên (vì tập lệnh này sử dụng tích hợp sẵn của Perl). Bạn có thể thấy một bản phác thảo về cách làm điều đó trong câu trả lời khác của tôi .


9
+1 - thực tế bạn quan tâm đủ để viết một kịch bản để làm điều đó thật tuyệt vời.
JRG

4
Thật tuyệt khi thấy rằng bạn đã dành thời gian để cung cấp một kịch bản cho việc này. Tuyệt quá!
samarasa

Vui vì bạn thích nó.
con-f-use

@ con-f-use sẽ thật tuyệt nếu bạn có thể phát hành nó trong giấy phép gpl.
Lincity

@Alaukik Giấy phép MIT cũng sẽ tốt với bạn. Nó dễ dàng hơn và tương thích GPL?
con-f-sử dụng

4

ObsessiveFOSS yêu cầu thực hiện Blum, et al. trình tạo số giả ngẫu nhiên an toàn bằng mật mã. Dưới đây là bản phác thảo của tôi về cách làm điều đó. Các mã khác vẫn giữ nguyên như trong câu trả lời trước đó của tôi . Người ta chỉ cần thay thế randomizeLabelchương trình con và chèn mã này thay thế:

use bigint;

# Kinda large primes
$p = 338047573;   # Any pair of large primes will suffice here...
$q = 4182249941;  #+...as long as they fullfill the congruence check below
$rand = 7;    # Seed for the random number generator (x_0 in the wiki)

sub errMsg {
    $dialog = Gtk2::MessageDialog->new($window, 'destroy-with-parent', 'error', 'ok', $_[0]);
    $dialog->signal_connect (response => sub { exit 1; });
    $dialog->run;
}

# Check congruence 3 mod 4 (for quadratic residue)
if( ($p-3)%4 == 0 ) { errMsg('Error: Variable p is ill choosen.'); }
if( ($q-3)%4 == 0 ) { errMsg('Error: Variable q is ill choosen.'); }
# Note: For large cycle lengths gcd(φ(p-1), φ(q-1)) should also be small,...
#+...where φ is Euler's totient function but this is not checked here

# Compute Modulus in Blum Blum Shub
$M = $p*$q;

sub randomizeLabel { # This does the actual randomization
    $min = int($entry1->get_text); $max = int($entry2->get_text); # Boundaries for the desired random range from the input filed of the GUI (included for convenience when modifying the script - not used here)

    # Blum Blum Shub pseudo random number generator
    $rand = ($rand*$rand) % $M;

    # Here you have to extract the bits and shift them in range
    $randout = $rand & (2**6-1); # Change this line. It's an example and extracts the five least significant bits! To extract the ten LSBs use '(2**11-1)' and so on...
    # $randout = ...$min...$max...; # shift it in the right range (not done here)

    $diplabel->set_markup( "<span size=\"$size\">$randout</span>" );
}

Như đã đề cập, nó không đầy đủ. Người ta sẽ phải sử dụng các toán tử bitwise để trích xuất các số ngẫu nhiên hữu ích, dịch chuyển và chia tỷ lệ chúng cho phù hợp giữa $min$max. Ngay bây giờ đầu vào tối thiểu và tối đa không được sử dụng.


Tôi nghi ngờ có một mô-đun Perl cho CSPRNG hoạt động tốt hơn kịch bản của tôi.
con-f-sử dụng

1

Nó có thể được thực hiện rất dễ dàng với QML ngày hôm nay:

import QtQuick 2.0
import Ubuntu.Components 0.1

Rectangle {
    id: mainView
    width: units.gu(30) 
    height: units.gu(40)
    Column {
        id: generator
        spacing: units.gu(1)
        anchors.horizontalCenter: mainView.horizontalCenter
        Text {
            id: ramdom_number
            text: "0"
            font.pointSize: 100
            anchors.horizontalCenter: generator.horizontalCenter
        }
        TextField {
            id:min
            text: "0"
        }
        TextField {
            id: max
            text: "100"
        }
        Button {
            text: "Generate!"
            width: generator.width
            onClicked: ramdom_number.text = Math.floor((Math.random()*(max.text-min.text+1))+min.text);
        }
    }
}

Chạy mã này với qmlscene:

nhập mô tả hình ảnh ở đây

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.