Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8708593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:10:23+00:00 2026-06-13T04:10:23+00:00

I intend to generate random number the following step: Read the data from file

  • 0

I intend to generate random number the following step:

  1. Read the data from file (<DATA>)
  2. Generate random numbers as many as the input data lines
  3. The random number should not be generated twice,
    e.g. if the rand number generated in loop no x, has been created
    before then, recreate the random number.

Here is the code I have which leads to infinite loop.
What’s wrong with my logic, and how can I fix it?

#!/usr/bin/perl -w
use strict;
my %chrsize = ('chr1' =>249250621);

# For example case I have created the
# repository where a value has been inserted.
my %done =("chr1    182881372" => 1);

while ( <DATA> ) {
 chomp;
 next if (/^\#/);

 my ($chr,$pos) = split(/\s+/,$_);
 # this number has been generated before
 # with this: int(rand($chrsize{$chr}));
 # hence have to create other than this one
 my $newst =182881372;

 my $newpos = $chr ."\t".$newst;


 # recreate random number
 for (0...10){
     if ( $done{$newpos} ) {

            # INFINITE LOOP
            $newst = int(rand($chrsize{$chr}));
            redo;
     }
 }

 $done{$newpos}=1;
print "$newpos\n";

}


__DATA__
# In reality there are 20M of such lines
# name  positions
chr1    157705682
chr1    19492676
chr1    169660680
chr1    226586538
chr1    182881372
chr1    11246753
chr1    69961084
chr1    180227256
chr1    141449512
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:10:25+00:00Added an answer on June 13, 2026 at 4:10 am

    You had a couple of errors:

    1. You were setting $newst within your loop every time, so $newpos never took on a new value.
    2. Your inner for loop didn’t make sense, because you never actually changed $newpos before checking the condition again.
    3. redo; was working on the inner loop.

    Here is a corrected version that avoids redo altogether.

    Update: I edited the algorithm a bit to make it simpler.

     #!/usr/bin/perl -w
    use strict;
    my $chr1size = 249250621;
    
    my %done;
    my $newst;
    
    while ( <DATA> ) {
        chomp;
        next if (/^\#/);
        my ($chr,$pos) = split(/\s+/,$_);
    
        my $newpos;
        #This will always run at least once
        do {
            $newst = int(rand($chr1size));
            $newpos = $chr ."\t".$newst;
        } while ( $done{$newpos} );
    
        $done{$newpos}=1;
        print "$newpos\n";
    }
    

    Update 2: while the above algorithm will work, it will get really slow on 20,000,000 lines. Here is an alternative approach that should be faster (There is sort of a pattern to the random numbers it generates, but it would probably ok for most situations).

    #!/usr/bin/perl -w
    use strict;
    my $newst;
    
    #make sure you have enough.  This is good if you have < 100,000,000 lines.
    use List::Util qw/shuffle/;
    my @rand_pieces = shuffle (0..10000);
    
    my $pos1   = 0;
    my $offset = 1;
    while ( <DATA> ) {
        chomp;
        next if (/^\#/);
        my ($chr,$pos) = split(/\s+/,$_);
    
        $newst = $rand_pieces[$pos1] * 10000 + $rand_pieces[($pos1+$offset)%10000];
        my $newpos = $chr ."\t".$newst;
    
        $pos1++;
        if ($pos1 > $#rand_pieces) 
        {
            $pos1 = 0;
            $offset = ++$offset % 10000;
            if ($offset == 1) { die "Out of random numbers!"; } 
        }
    
        print "$newpos\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I intend to develop a J2ME application, that should be able to read words
I am trying with below code to generate 10 digits unique random number. As
I intend to create asp.net pages using Visual Studio 2008. Preferably, the pages should
I intend for part of a program I'm writing to automatically generate Gaussian distributions
I'm having trouble generating random numbers that do not follow a discrete uniform distribution.
I would like to generate some images dynamicaly. For that, I intend to create
I intend to generate JSON that's like this: { stores: [{Name:abc,Phone,1234567},{Name:def,Phone,1111111}], numResults: 2 }
I would like to generate a text file containing all 19,683 Tic-Tac-Toe board layouts
I'm having touble in making my random generated image (which is read as a
Can i provide some algorithm for generate smth intead of FK030HF9840303 for example?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.