473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

random numbers

I have a problem that I can't seem to solve. So, maybe there is someone out there who can help.

Here is my problem. I have a file with a certain number of entries in it. Each entry is a one line in the file. I have a certain number of users. How can I write a Perl code that will divide the number of entries randomly between the users. Every time the code is executed, a new set is generated. For example, I have a file with 20 entries and 5 users. Dividing equally, each user gets 4 entries. Divided randomly,
users 1 gets 8, user 2->3, user 3->3, user 4->2 and user 5->4. Next execution something different, but the total of entries is always equal to 20. And all the users get a share.

Thanks.
Jun 12 '07 #1
5 1804
KevinADC
4,059 Expert 2GB
What have you tried so far?
Jun 12 '07 #2
miller
1,089 Expert 1GB
There are lots of different ways to do this. What have you tried so far?

Obviously this will take some type of algorithm. But any solution will probably simply need one of the following resources:

perldoc rand
cpan List::Util - The shuffle function

- Miller
Jun 12 '07 #3
KevinADC
4,059 Expert 2GB
Usually I don't post code unless you have shown some effort, but the question was interesting enough that I thought about it and came up with a very simple method:

Expand|Select|Wrap|Line Numbers
  1. my @users = qw(john joe jane jerry janet);
  2. my @data = (foo) x 20;
  3. my %users = map {$_ => []} @users;
  4. for (@data) {
  5.    push @{$users{$users[int rand @users]}},$_;
  6. }
  7. foreach my $user (@users) {
  8.    print "$user = @{$users{$user}}\n";

This works by picking a random user to get some data each time through the "for" loop. There is no need to check how many users there are or how much data there is. This might not actually be what you want because sometimes a user can have zero data and there is a very very small possibility that one user could get all the data. But it would be easy to make sure each user had at least one bit of data or check for a maximum amount of data per user.
Jun 13 '07 #4
miller
1,089 Expert 1GB
I agree about this type of problem being interesting. Here's a slight modification to the code that you provided that balances out the distribution across all users.

Expand|Select|Wrap|Line Numbers
  1. my @users = qw(john joe jane jerry janet);
  2. my @data = (1..20);
  3. my %users = map {$_ => []} @users;
  4. for (my $i = 0; @data; $i = ($i + 1) % @users) {
  5.     push @{$users{$users[$i]}}, splice @data, int(rand @data), 1;
  6. }
  7. foreach my $user (@users) {
  8.     print "$user = @{$users{$user}}\n";
  9. }
  10.  
Typical output:
Expand|Select|Wrap|Line Numbers
  1. >perl scratch.pl
  2. john = 15 3 14 8
  3. joe = 5 4 1 12
  4. jane = 19 11 18 20
  5. jerry = 9 10 17 6
  6. janet = 13 2 7 16
  7.  
- Miller
Jun 13 '07 #5
KevinADC
4,059 Expert 2GB
Cool code. :)


........................................
Jun 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
5
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.