Page 1 of 1

OneRandomSerie

Posted: Sun May 01, 2011 11:58 am
by jmburnod
Hi all,

I need a function to get a random list of n lines with n2 lines have the same number between 1 and n3
I write it but i'm sur someone have a more elegant way :D

Code: Select all

function OneRandomSerie pNbTot,pNbM,pNbFig 
   --•• return a random list of pNbTot lines with PnbM lines have the same number between 1 and pNbFig
   put empty into rUneSerie
   put random(pNbFig) into tModele --••
   repeat pNbM
      put "UnpNbM"&tModele&return after rUneSerie
      wait 1 milliseconds
   end repeat
   put pNbTot-pNbM into nbl
   put round(pNbM/pNbFig)+1 into nb
   repeat nb
      repeat with i = 1 to pNbFig
         if i = tModele then next repeat
         put i&return after rUneSerie
         wait 1 milliseconds
      end repeat
   end repeat
   put line 1 to pNbTot of rUneSerie into rUneSerie
   sort rUneSerie by random(pNbTot)
   return rUneSerie
end OneRandomSerie
Thank one more

Jean-Marc

Re: OneRandomSerie

Posted: Sun May 01, 2011 6:02 pm
by jmburnod
Hi All,

Forget my previous message, this script work best

Code: Select all

function OneRandomSerie pNbTot,pNbM,pNbFig 
   --•• return a random list of pNbTot lines with PnbM lines have the same number between 1 and pNbFig
   put random(pNbFig) into tModele --••
   
   put empty into rUneSerie
   repeat pNbM
      --put "UnpNbM"&tModele&return after rUneSerie --•• pour dev
      put tModele&return after rUneSerie
      wait 1 milliseconds
   end repeat
   
   put (pNbTot-pNbM)-1 into nbl
   put round(pNbM/pNbFig)+1 into nb
   
   put empty into tAutres
   repeat with i = 1 to pNbFig
      put i&return after tAutres
      wait 1 milliseconds
   end repeat
   
   filter tAutres without tModele
   put the num of lines of tAutres into nbl
   
   put round(pNbTot/nbl) + 1 into NbT
   repeat NbT
      put tAutres&return after rUneSerie
      wait 1 milliseconds
   end repeat
   
   put line 1 to pNbTot of rUneSerie into rUneSerie
   sort rUneSerie by random(pNbTot)
   return rUneSerie
end OneRandomSerie
All the best

Jean-Marc

Re: OneRandomSerie

Posted: Sun May 01, 2011 10:40 pm
by BvG
I don't understand your code, or your description of what you want to do. Nontheless, I made this:

Code: Select all

function createPseudoRandomList totalLines,linesWithNumbers,maxAllowedNumber
   -- first create those lines witouth numbers
   put totalLines - linesWithNumbers into emptyLineCount
   repeat for emptyLineCount times
      put return after theResult
   end repeat
   -- create lines that have numbers
   repeat for linesWithNumbers times
      put random(maxAllowedNumber) & return after theResult
   end repeat
   --need to delete last return, otherwise there's a line too much
   delete char -1 of theResult
   --randomly order the resulting list, to mix empty lines and numbered ones
   sort theResult by random(the number of lines in theResult)
   --done!
   return theResult
end createPseudoRandomList
General notes: waiting for 1 millisecond within a repeat loop slows your code down A LOT!!!

Repeat for each <chunk type> is the fastest way to do repeat loops, but here there's no easy way to use them.

Re: OneRandomSerie

Posted: Mon May 02, 2011 6:34 pm
by jmburnod
Hi BvG
And thank for your reply
I try to explane what i want

pNbTot = the number of lines i want
pNbM = the num of lines have the same number choosed between 1 and pNbFig
pNbFig = the maxnum of choices

For instance (i use item instead lines)

put OneRandomSerie(6,2,5) return "1,3,2,2,4,5) if tModele = 2

It work well for me but i'm sur there is a best way

Best regards

Jean-Marc

Re: OneRandomSerie

Posted: Mon May 02, 2011 7:15 pm
by BvG
Ah so no empty lines.

What do you want to do when the numbers don't match up? for example 10,2,3 could produce "1,1,2,2,3,3" but that's not 10 lines at all?

Re: OneRandomSerie

Posted: Mon May 02, 2011 9:02 pm
by jmburnod
Yes, no empty line
What do you want to do when the numbers don't match up? for example 10,2,3 could produce "1,1,2,2,3,3" but that's not 10 lines at all?
No. 10,2,3 produce for instance "3,2,1,2,1,3,2,1,1,2" (in this case tModele = 3)

The important points are
1. The list have pNbTot lines
2. pNbM line with the same number between 1 and pNbFig
3. The others lines with a number between 1 and pnbFig and <> tModele

Re: OneRandomSerie

Posted: Tue May 03, 2011 12:07 am
by BvG
you're really bad at explaining this :P

why do you introduce another variable suddenly? now it's 4 paremeters (pNbNot, pNbM, pNbFig, tModele), instead of 3?

Also, the main problem is just not explained because "pNbM line with the same number between 1 and pNbFig" does not mean anything to me. Next try of an assumption:

There will be a certain amount of lines, specified in pNBnot.
You want random numbers beteen 1 and maxNumberSize, which you call pNbFig.
In addition you have a rule about one of the random numbers appearing a certain time (pNbM)

i've coded according to the above interpretation of what you say:

Code: Select all

function semiRandomNumbers numberOfLines, maxRandomNumber, specialNumber, amountOfSpecialNumbers
   --first the special number
   repeat for amountOfSpecialNumbers times
      put specialNumber & return after theResult
   end repeat
   --now the rest
   repeat for numberOfLines - amountOfSPecialNumbers times
      put specialNumber into nextNumber
      repeat until nextNumber <> specialNumber
         put random(maxRandomNumber) into nextNumber
      end repeat
      put nextNumber & return after theResult
   end repeat
   --again one return too much
   delete char -1 of theResult
   --randomize
   sort theResult by random(the number of lines in theResult)
   return theResult
end semiRandomNumbers

Re: OneRandomSerie

Posted: Wed May 04, 2011 12:28 pm
by jmburnod
Sorry for the bad explanantion.
And thank for your help
why do you introduce another variable suddenly? now it's 4 paremeters (pNbNot, pNbM, pNbFig, tModele), instead of 3?
No. Only 3 params, "tModele"(specialNumber in this script) is a variable

I changed a part of your script. I want have the maximum of different numbers in the list tOtherNumbers. Random can't do it with a small maxRandomNumber

This script work well for me.

Code: Select all

function semiRandomNumbers numberOfLines, maxRandomNumber, amountOfSpecialNumbers
   --first the special number
   put random(maxRandomNumber) into specialNumber
   repeat for amountOfSpecialNumbers times
      put "*"&specialNumber & return after rsemiRandomNumbers --••"*" to see  specialNumber
   end repeat
   repeat with i = 1 to maxRandomNumber --•• how to avoid this "repeat with" ?
      if i <>specialNumber then
         put i &return after tOtherNumbers
         wait 1 milliseconds
      end  if
   end repeat
   delete char -1 of tOtherNumbers
   --•• Add tOtherNumbers to the rsemiRandomNumbers (not any line)
   put amountOfSpecialNumbers into tNumLine
   repeat until tNumLine > numberOfLines
      put tOtherNumbers &return after rsemiRandomNumbers
      add (maxRandomNumber-1) to tNumLine
   end repeat 
   --•• maeby the num of lines of rsemiRandomNumbers > numberOfLines
   put line 1 to numberOfLines of rsemiRandomNumbers into rsemiRandomNumbers
   sort rsemiRandomNumbers by random(numberOfLines)
   return rsemiRandomNumbers
end semiRandomNumbers
Jean-Marc