DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: IDEA: Randomly select 'n' Lines from text list  (Read 12,908 times)

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 180
IDEA: Randomly select 'n' Lines from text list
« on: December 18, 2011, 07:28 AM »
Simple idea, I think. :) Pulling names from a digital Hat

Provide a scrollable text area for the user to type of paste a list, each line being a new choice

Provide a means to define how many items to randomly select

Provide a second scrollable text area to display the results from the randomised selection, with a numerical identifier (for reference) and in their order of selection


Although I have found facilities on the net to, randomly select an item from a list. They seem only to pull 1 item, and there is a security risk of passing the list across the net


Why an I Magician62? Because Magician1 thru 61 were gone. :)

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #1 on: December 18, 2011, 11:34 AM »
Doesn't mouser have such an application for drawing winners of the raffles he held before? ;D
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #2 on: December 18, 2011, 11:43 AM »
i do have a pretty powerful tool for this, Prize Optimizer (written about here), which does simulated annealing to optimize the distribution of multiple prizes to multiple people based on their rankings.

But it's never been released to public.. only because it gets its data from parsing email and i'd have to do a little bit of work to make it possible to manualy enter input data.

One one hand it's very useful as is since it will parse email entries with ranking and use them to create data, and then generate output emails notifying people what they won if anything.

With a bit of work i could make it so input could be done interactively.. but it's hard to justify if there are only 3 people on the planet who have any use for it.. seems like something that might be better done in modern times using an online web service..

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #3 on: December 18, 2011, 11:45 AM »
Would be a very nice NANY entry :Thmbsup:
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

ewemoa

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 2,922
    • Plugin Coding Medal
    • Participated in NANY Challenge Three Times
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #4 on: December 18, 2011, 01:12 PM »
I'm thinking of putting the following into LBA (a Clipboard Help and Spell custom tool) -- haven't tested it much so it might be off.  I don't expect it to work well on a large number of choices, but who knows?

Setup would include installing and configuring LBA and putting the following in LBA's script folder.

To use, open an editor and put one choice on each line (and save a copy somewhere else).  Then select all of the choices, invoke LBA, and choose the menu entry associated with the saved script.  A dialog box should appear asking for how many items to choose and then afterwards the selected text should be replaced by an appropriate number of items.

Code: (autohotkey)
ChangeText(InputText)
{
  Lines := []
  Pattern := "mO)^(.*)$"
  Start := 1
  Cnt := 0
  While (True)
  {
    FoundPos := RegExMatch(InputText, Pattern, mObj, Start)
    If (ErrorLevel != 0)
    {
      OutputDebug, % "RegExMatch ErrorLevel: " . ErrorLevel
      SoundBeep
      Return
    }
    If (FoundPos == 0)
    {
      Break
    }
    Start := FoundPos + 1
    Lines.Insert(mObj[1])
    Cnt := Cnt + 1
  }
  If (Cnt == 0)
  {
    Message := "Sorry, didn't find any lines"
    OutputDebug, % Message
    MsgBox, % Message
    SoundBeep
    Return
  }
  Title := "How many choices out of " . Cnt . "?"
  Prompt := "Found " . Cnt . " items.`n`n"
          . "How many to choose (1 - " . Cnt . ") ? "
  InputBox, NChoices, % Title, % Prompt
  If (ErrorLevel == 1)
  {
    OutputDebug, % "Canceled"
    SoundBeep
    Return
  }
  If (NChoices is not integer)
  {
    OutputDebug, % "Value not integer: " . NChoices
    MsgBox, % "Please specify an integer (whole number)."
    Return
  }
  If (NChoices <= 0) or (NChoices > Cnt)
  {
    OutputDebug, % "Value not between 1 and " . Cnt . ": " . NChoices
    MsgBox, % "Please choose a number between 1 and " . Cnt . "."
    Return
  }
  ResultStr := ""
  Loop, % NChoices
  {
    MaxIndex := Lines.MaxIndex()
    Random, Value, 1, % MaxIndex
    ResultStr .= Lines[Value] . "`n"
    Lines.Remove(Value)
  }
  Return ResultStr
}

barney

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,294
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #5 on: December 18, 2011, 06:10 PM »
With a bit of work i could make it so input could be done interactively.. but it's hard to justify if there are only 3 people on the planet who have any use for it.. seems like something that might be better done in modern times using an online web service..

Actually, I could see a use for this as an option in CHS, but that might be pretty specific to my usages.  Problem with Web services - apart from my paranoia - is that too many times I'm not, or unable to get, online.
Make a good day ... barn

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #6 on: December 18, 2011, 06:26 PM »
Give this a shot:  RandomLister

I wasn't sure if you wanted the results numbered sequentially or their actual line numbers from the source.

2011-12-18_122417.png

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 180
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #7 on: December 18, 2011, 09:53 PM »
skwire
The original concept was the results numbered sequentially, but as they appear on screen in order they are selected, its not necessary.

This meets the design requirement very well, many thanks.

ewemoa
I installed CHS and LAB, give me a month and I might have figured them out :)
Why an I Magician62? Because Magician1 thru 61 were gone. :)

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #8 on: December 18, 2011, 10:21 PM »
This meets the design requirement very well, many thanks.

You're welcome.  That was more of a proof-of-concept i.e. it's not resizable and doesn't save any settings.  Did you want these bits added?

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 180
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #9 on: December 18, 2011, 10:28 PM »
Not really necessary, as each usage would likely use a different data set.

Although resizing might be good for very large lists, its only really important to cases where the output is larger, and even then its not of major importance
Why an I Magician62? Because Magician1 thru 61 were gone. :)

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #10 on: December 19, 2011, 12:00 AM »
Although resizing might be good for very large lists
Please download download RandomLister again.  The app should be resizable now.

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 180
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #11 on: December 20, 2011, 09:30 PM »
Excellent!

Now using your telepathy, what is the next idea I have in mind for a simple utility? :)
Why an I Magician62? Because Magician1 thru 61 were gone. :)

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #12 on: December 20, 2011, 09:35 PM »
Now using your telepathy, what is the next idea I have in mind for a simple utility?

An app to do all of your holiday shopping with gift-wrapping as an option?  :P

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 180
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #13 on: December 20, 2011, 10:04 PM »
That would be brilliant, but you forgot the bit where it charges it to Santa's account, and not mine :)
Why an I Magician62? Because Magician1 thru 61 were gone. :)

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Randomly select 'n' Lines from text list
« Reply #14 on: December 21, 2011, 04:04 PM »
That would be brilliant, but you forgot the bit where it charges it to Santa's account, and not mine :)

I'll add it to the ToDo list.  =]

Original address: https://www.donationcoder.com/forum/index.php?topic=29325.0