http://qs321.pair.com?node_id=1226862


in reply to Re: Make random numbers
in thread Make random numbers

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" ;

Replies are listed 'Best First'.
Re^3: Make random numbers
by Tux (Canon) on Dec 07, 2018 at 07:38 UTC

    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;
Re^3: Make random numbers
by hdb (Monsignor) on Dec 07, 2018 at 09:23 UTC

    Here is my code:

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