Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 45

Thread: What's that code snippet?

  1. Top | #11
    Elder Contributor barbos's Avatar
    Join Date
    Nov 2005
    Location
    Mlky Way galaxy
    Posts
    11,673
    Archived
    8,047
    Total Posts
    19,720
    Rep Power
    69
    Quote Originally Posted by Swammerdami View Post
    Quote Originally Posted by Jayjay View Post
    I agree that the commented out return statement is not equivalent to the one that's not. The former checks if the decimal part of f is larger than splitp, implying that splitp is a floating point between 0 and 1. The latter checks if f is greater than splitp. Hmm.
    This was addressed in the post immediately before yours. Let me be more explicit:

    (1) item_rand() utilizes TWO random numbers: (a) an integer in the range {0, 1, 2, ..., size-1} and a "splitting" probability 0 < p < 1.
    (2) item_rand() derives BOTH the random numbers it needs from a SINGLE call to a random-number generator.
    (3) Since item_rand() may be called millions of times while setup() is called only once, a TINY bit of time can be saved by adding K to R[K].rh_splitp. Obviously setup() and item_rand() must agree on whether this micro-optimization is in use or not! setup() is a MUCH longer routine than item_rand() and is NOT shown.

    The optimizations (2) and (3) are much less interesting than the algorithm implemented by setup()/item_rand(). It can be used by MANY simulation programs and does what it does MUCH faster than a naive approach.
    Optimized Monte-Carlo generator?

  2. Top | #12
    Veteran Member
    Join Date
    Dec 2017
    Location
    Land of Smiles
    Posts
    1,068
    Rep Power
    15
    More snippets please! Surely there are lots of code snippets that are amusing, instructive, scary, puzzling, disgusting or worth looking at for some reason!
    I think Jayjay's Mondrian snippet prints "Hello world." Am I close?


    Quote Originally Posted by barbos View Post
    Quote Originally Posted by Swammerdami View Post
    ... the algorithm implemented by setup()/item_rand() [] can be used by MANY simulation programs and does what it does MUCH faster than a naive approach.
    Optimized Monte-Carlo generator?
    Yes!
    Among how many distinct arbitrary-probability items will item_rand() choose from?
    (I barely remember my English. Is "choose from among" an English idiom? Did I illegally split a preposition?)

    The following was output by a program that called item_rand() for each character. What did that program do? Be specific.
    To say,
    'Thy past know,
    Feed'st is't not my still shown.
    To whom ther's gainst that kept strange, had night,
    For them thin my loss, he roses return'd loves test high defections fix'd that pour'st though win my heart,
    As he leave memory
    My spirit, now untains as play as your nature hap to the golder ther with my truth defection the was those jacks truth's and mock to hath canst my in tabless bold,
    Bare flower pale same;
    And hooks, that thy name to shadows I be fair far that their special neight in thee hourse her with from their body's ending thy fate,
    And not by all my babe, youthful doth flies
    When so sullence he trial love to the posters brain widow'd chaste me I not love which his grow shall carcely and error and it lame, thou show!

    Thou pine and the thought kill I saw you not beauty grief, all mayst hope.

    Like a heart stransfix the word
    No profane, and age,
    Which glory;
    That a fortify the guides to large? Is thy powers with taugh'd each borrows not could many lamb he leaguest, 'tis fell keeps your never speed,
    The worst others been
    To spends
    For if thou not keep,
    Laid best in my loves, thy see them bore thus vainly charg'd the eyes shall be deceiv'd;
    But yet whit doth fingers not able arigour fix'd'?
    Be scandles their we, what thoughts of my lays shame,
    Commit the world with three shall I still I this writ and stol'n from the love ere iniquid place?
    I have his purple same;
    Coral be so it thier wrongs
    That shady sport.

    There I boast thou did example married, delight,

  3. Top | #13
    Elder Contributor barbos's Avatar
    Join Date
    Nov 2005
    Location
    Mlky Way galaxy
    Posts
    11,673
    Archived
    8,047
    Total Posts
    19,720
    Rep Power
    69
    Where is my Prize then?

    Can I at least get a job at Google?

  4. Top | #14
    Veteran Member
    Join Date
    Dec 2017
    Location
    Land of Smiles
    Posts
    1,068
    Rep Power
    15
    Quote Originally Posted by barbos View Post
    Where is my Prize then?
    Prizes for best SUBMISSIONS only. So far, Jayjay is in in 1st place with his Mondrian snippet. I'm tied with myself for 2nd.

    Can I at least get a job at Google?
    I dunno; can you?

  5. Top | #15
    Elder Contributor barbos's Avatar
    Join Date
    Nov 2005
    Location
    Mlky Way galaxy
    Posts
    11,673
    Archived
    8,047
    Total Posts
    19,720
    Rep Power
    69
    OK, I submit... let's see what I have here,...... I submit my avatar.

  6. Top | #16
    Elder Contributor barbos's Avatar
    Join Date
    Nov 2005
    Location
    Mlky Way galaxy
    Posts
    11,673
    Archived
    8,047
    Total Posts
    19,720
    Rep Power
    69
    Quote Originally Posted by Swammerdami View Post

    Snippet #3


    17/91; 78/85; 19/51; 23/38; 29/33; 77/29; 95/23; 77/19; 1/17; 11/13; 13/11; 15/2; 1/7; 55/1
    I am not a Mensa material.

  7. Top | #17
    Veteran Member
    Join Date
    Dec 2017
    Location
    Land of Smiles
    Posts
    1,068
    Rep Power
    15
    Quote Originally Posted by barbos View Post
    Quote Originally Posted by Swammerdami View Post

    Snippet #3


    17/91; 78/85; 19/51; 23/38; 29/33; 77/29; 95/23; 77/19; 1/17; 11/13; 13/11; 15/2; 1/7; 55/1
    I am not a Mensa material.
    That "tiny" program contains the Sieve of Eratosthenes; and outputs the entire sequence of prime numbers!
    (In the form 22, 23, 25, 27, 211, ... with non-power-of-two numbers ignored. ... If you wait long enough.)

    Who created that "programming language"? Google "Genius who died of Covid-19"

  8. Top | #18
    Contributor
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    6,613
    Rep Power
    20
    char conundrum(char x){
    if( (x >= 65) && (x <= 90) ) return (x | 0x20);
    if( (x >= 97) && (x <= 122) ) return x(x & 0xDF);
    return(x);
    }

    0x is hexadecimal

  9. Top | #19
    Super Moderator
    Join Date
    Sep 2000
    Location
    Nevada
    Posts
    31,466
    Archived
    96,752
    Total Posts
    128,218
    Rep Power
    100
    Quote Originally Posted by steve_bank View Post
    char conundrum(char x){
    if( (x >= 65) && (x <= 90) ) return (x | 0x20);
    if( (x >= 97) && (x <= 122) ) return x(x & 0xDF);
    return(x);
    }

    0x is hexadecimal
    I'm pretty sure this flips the case of the character, doesn't alter anything else.

    And anyone that can follow that code would know what 0x means.

  10. Top | #20
    Contributor
    Join Date
    Apr 2002
    Location
    Finland
    Posts
    5,111
    Archived
    6,261
    Total Posts
    11,372
    Rep Power
    76
    ... and there is a typo, x.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •