GHMON has asked for the wisdom of the Perl Monks concerning the following question:
This node falls below the community's threshold of quality. You may see it by logging in.
Re: Make random numbers
by Eily (Monsignor) on Dec 06, 2018 at 11:00 UTC
|
| [reply] |
|
#!/usr/bin/perl
use strict;
use warnings;
print "",int(rand(1000))+1,"\n" for (1..100);
| [reply] [d/l] [select] |
|
I'm tempted to argue that the OP didn't mention wanting integer numbers between 1 and 1000 :P, but technically I did understand the same as you until I started thinking about an answer to your post :D. I didn't mention int on purpose for two reasons, I knew it was mentioned in the rand page, and I expected GHMON to come back here for corrections if the first attempt didn't work as expected.
| [reply] |
|
|
| A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: Make random numbers
by Corion (Patriarch) on Dec 06, 2018 at 11:44 UTC
|
Hi GHMON!
Please show us the code you have already written, and also show us how your code fails for you. This helps us provide you with much better answers, as we can address the exact problem you have.
Others have already given you links to the relevant parts of this solution, so maybe now you can start and show us the code you have, apply the solutions you were given and then tell us where the code doesn't do what you need.
| [reply] |
Re: Make random numbers
by hdb (Monsignor) on Dec 06, 2018 at 12:21 UTC
|
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 )
| [reply] |
|
| [reply] |
|
| [reply] |
|
#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" ;
| [reply] [d/l] |
|
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
| [reply] [d/l] |
|
|
my @array = map { 1+int(rand(1000)) } 1..100;
| [reply] [d/l] |
Re: Make random numbers
by stevieb (Canon) on Dec 06, 2018 at 20:13 UTC
|
use warnings;
use strict;
use Inline 'C';
my @random_numbers = getRand(1, 1000, 100);
print "$_\n" for @random_numbers;
__END__
__C__
#include <stdlib.h>
void getRand (int start, int end, int iterations){
time_t t;
srand((unsigned) time(&t));
inline_stack_vars;
inline_stack_reset;
int i;
for (i = 0; i < iterations; i++){
int randomNum = rand() % (end - start);
inline_stack_push(sv_2mortal(newSViv(randomNum)));
}
inline_stack_done;
}
| [reply] [d/l] |
|
use 5.14.2;
use warnings;
use Inline "C";
say for getRand (50, 1000, 10);
__END__
__C__
#include <stdlib.h>
void getRand (int start, int end, int iterations) {
time_t t;
srand ((unsigned)time (&t));
inline_stack_vars;
inline_stack_reset;
int i;
for (i = 0; i < iterations; i++) {
int randomNum = rand () % (end - start);
inline_stack_push (sv_2mortal (newSViv (start + randomNum)));
+/* Add start to random number */
}
inline_stack_done;
}
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] [select] |
|
| [reply] |
Re: Make random numbers
by hippo (Bishop) on Dec 06, 2018 at 11:11 UTC
|
Which part of this task are you having problems with, precisely?
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: Make random numbers
by thanos1983 (Parson) on Dec 07, 2018 at 11:23 UTC
|
Hello GHMON,
You have received several answers to your question. Just for fun another approach.
Initially you create an array of 1 - 1000 values. Then you use the random function to choose an element from this array and the same time when you choose an element you remove it from the array to make sure on your final array you will not have it again :)
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @original = 1 .. 1000;
# print Dumper \@array;
my @array;
push @array, splice(@original, rand @original, 1) for (1 .. 100 );
print Dumper \@array;
__END__
$VAR1 = [
848,
909,
62,
.
.
.
etc
If you really want to push things even more (not that makes a difference but just for fun). You can shuffle the original array before you choose the value randomly:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use List::Util qw(shuffle);
my @original = 1 .. 1000;
# print Dumper \@array;
@original = shuffle(@original);
my @array;
push @array, splice(@original, rand @original, 1) for (1 .. 100 );
print Dumper \@array;
__END__
$VAR1 = [
319,
793,
69,
571,
.
.
.
etc
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
| [reply] [d/l] [select] |
Re: Make random numbers
by bliako (Monsignor) on Dec 07, 2018 at 11:50 UTC
|
every time you request a random number an angel is dying somewhere, that's my theory
| [reply] |
|
| [reply] |
|
every time you request a random number an angel is dying somewhere, that's my theory
If only killing gods was that easy ...
Cheers, Rob
| [reply] |
|
If only killing gods was that easy ...
it's all in the mind ... | [reply] |
|
|