Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Make random numbers

by GHMON (Novice)
on Dec 06, 2018 at 10:50 UTC ( [id://1226825]=perlquestion: print w/replies, xml ) Need Help??

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.

Replies are listed 'Best First'.
Re: Make random numbers
by Eily (Monsignor) on Dec 06, 2018 at 11:00 UTC
      I did like your post. This should have been enough to get the OP started.

      There are 2 minor details to be addressed. The rand(1000) function will return float values from 0 and never reaching 1000. So we need to chop off the fractional part - that will yield ints from 0..999. To get the desired range, adding a constant offset of 1 will do the job.

      #!/usr/bin/perl use strict; use warnings; print "",int(rand(1000))+1,"\n" for (1..100);

        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.

    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.

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 )

      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

        Here is my code:

        my @array = map { 1+int(rand(1000)) } 1..100;
Re: Make random numbers
by stevieb (Canon) on Dec 06, 2018 at 20:13 UTC

    Here's one example:

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

      Almost correct :) You need to add start to the return value:

      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

        ...I knew I was missing something and was off a tiny bit. This explains it. Thanks! :D

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?

    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!
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

      or John van Neumann rolls over in his grave. I think we've come a long way with pseudorandoms....

      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
         If only killing gods was that easy ... 

        it's all in the mind ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1226825]
Approved by erzuuli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found