Perl: the Markov chain saw PerlMonks

### Re: Make random numbers

by hdb (Monsignor)
 on Dec 06, 2018 at 12:21 UTC ( #1226839=note: print w/replies, xml ) Need Help??

in reply to Make random numbers

Like this?

( 290, 293, 714, 658, 219, 831, 173, 918, 966, 690, 255, 825, 374, 291, 340, 826, 855, 642, 171, 875, 492, 149, 64, 932, 176, 340, 348, 774, 789, 424, 311, 278, 984, 93, 436, 829, 580, 452, 216, 19, 108, 511, 317, 271, 538, 125, 373, 574, 720, 799, 206, 959, 570, 257, 735, 891, 527, 187, 371, 216, 323, 68, 161, 843, 585, 478, 484, 784, 311, 801, 565, 748, 741, 726, 590, 905, 669, 458, 709, 12, 703, 36, 706, 529, 534, 667, 611, 901, 724, 489, 46, 648, 48, 663, 578, 432, 927, 195, 865, 749 )

Replies are listed 'Best First'.
Re^2: Make random numbers
by soonix (Canon) on Dec 06, 2018 at 16:10 UTC
Re^2: Make random numbers
by GHMON (Novice) on Dec 07, 2018 at 06:31 UTC

Hi friend , this is my code

```#Hi First practice for PR class
use warnings ;
use strict ;

print 'First Number > ' ;
chomp(my \$A = <STDIN>) ;

print 'Second Number > ' ;
chomp(my \$B = <STDIN>) ;

my @range = (\$A .. \$B) ;
my \$rndn = \$range[rand(@range)] ;
my @points ;
my \$counter = 0 ;
while (\$counter <= 100) {
\$rndn ;
push @{\$points[\$counter]},\$rndn ;
\$counter++ ;
}

print @points, "\n" ;

That is not very efficient code. Here's my stab at your approach:

```use 5.14.2;
use warnings;

\$| = 1;

print "Range bottom > ";
chomp (my \$from = <STDIN>);

print "Range top    > ";
chomp (my \$to   = <STDIN>);

\$from =~ m/^\s*(-?\d+)\s*\$/ or die "Invalid range";
\$from = +\$1;
\$to   =~ m/^\s*(-?\d+)\s*\$/ or die "Invalid range";
\$to   = +\$1;
\$from <= \$to                or die "Invalid range";

my \$range_length = \$to - \$from + 1;

my @points = map { \$from + int rand \$range_length } 1 .. 100;
say "@points";

(do you see how many useful comments you get when you show effort?)

Enjoy, Have FUN! H.Merijn

Hi TNX bro i have a new code that calculate the mean of random number and then calculate the variance , so make gaussian function

```#Hi First practice for PR class
use warnings ;
use strict ;
use GD::Graph::linespoints ;
#use DBI ;
#use utf8 ;
#use Encode ;

#my \$sfile = '/root/source.txt' ;

print 'First Number > ' ;
chomp(my \$A = <STDIN>) ;

print 'Second Number > ' ;
chomp(my \$B = <STDIN>) ;

#my @range = (\$A..\$B) ;
#my \$rndn = \$range[int (rand(@range))] ;
my @points = () ;
my \$counter = 0 ;
while (\$counter <= 999) {
my \$rndn = \$A + (int rand(\$B - \$A + 1));
push (@points,\$rndn) ;
\$counter++ ;
}

#Calculate the Mean and Variance

my \$running_sum = 0;
my \$meansum ;
my \$vari ;
my \$element1 ;
my \$element2 ;

foreach \$element1 (@points) {
\$meansum += \$element1;
}

my \$mean = \$meansum/1000 ;
print "MEAN = \$mean" , "\n" ;

foreach \$element2 (@points) {
\$vari += ((\$meansum - \$element2)^2) ;
}

my \$variance = (\$vari/1000) ;
print "Variance = \$variance" , "\n" ;

print 'Second Number > ' ;
chomp(my \$xi = <STDIN>) ;

my \$Gs1 = (1/sqrt(2*3.14*\$variance)) ;
my \$Gs2 = 2.718^(-((\$xi - \$mean)^2)/(2*\$variance)) ;
my \$Go = \$Gs1 * \$Gs2 ;

print "\$Gs1" , "\n" ;
print "\$Gs2" , "\n" ;
print "\$Go" , "\n" ;

my \$graph = new GD::Graph::linespoints(2000 , 2000) ;
\$graph->set(
x_label => 'Points' ,
x_label_skip => 1 ,
y_label => 'Number' ,
y_label_skip => 1 ,
title => 'Time Vs Fee') or warn \$graph->error ;

\$graph->plot(\@points);
open OUT,'>','FirstPracticeTest.jpeg' or die "\$!";
binmode OUT;
print OUT \$graph->gd->jpeg;

Here is my code:

```my @array = map { 1+int(rand(1000)) } 1..100;

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1226839]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-05-22 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you prefer to work remotely?

Results (78 votes). Check out past polls.

Notices?