Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Generate a # between 1 and 50 in groups of 5

by pme (Monsignor)
on Jan 30, 2015 at 19:02 UTC ( [id://1115117]=note: print w/replies, xml ) Need Help??


in reply to Generate a # between 1 and 50 in groups of 5

Hi Hayest, welcome to the monastery.

Random numbers can be generated using rand() function (see rand). This short script below prints 5 random numbers between 1 and 50. The group of numbers is sorted.

#!/usr/bin/perl -w use strict; my %num; while ((keys %num) < 5) { $num{int(rand(50)+1)} = 1; } print join(' ' , sort {$a <=> $b} keys %num) . "\n";

Replies are listed 'Best First'.
Re^2: Generate a # between 1 and 50 in groups of 5
by Hayest (Acolyte) on Feb 04, 2015 at 15:27 UTC
    pme,

    This is exactly what I was looking to do. Now we are tasked with something additional: "Sum each group and display the results so that each number is on its own line." How would I go about doing something like this? Would I need to implement some sort of do/while or for/next loop?

    Thank you in advance!
      > How would I go about doing something like this?

      perlintro is a good entry point to learn programing!

      Cheers Rolf

      PS: Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-04-19 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found