Thread: Random

    #1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    Random


    can anyone tell me how i can make the rand ( ) funtion select different messages everytime it loads? or on an event.. or maybe a tutorial will do so i can learn my self
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Step one, get some random numbers:

    Code:
    >>> import random
    >>> random_generator = random.Random()
    >>> 
    >>> random_generator.randint(0, 10)
    4
    >>>
    Step two, store and pick some random messages:

    Code:
    >>> messages = ['Hello',
    'Message1',
    'Message2' ]
    >>>
    >>> tmp = random_generator.randint(0, len(messages)-1)
    >>> print messages[tmp]
    'Message1'
  4. #3
  5. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    sorta like php thanx
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    Or you could use the choice function:
    Code:
    import random
    messages = ['Hello', 'World', 'Happy', 'Christmas']
    
    print random.choice(messages)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Even better.

    I knew of random.shuffle(list), but not .choice.

IMN logo majestic logo threadwatch logo seochat tools logo