|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#2
|
|||
|
|||
|
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' |
|
#3
|
||||
|
||||
|
sorta like php
thanx |
|
#4
|
|||
|
|||
|
Or you could use the choice function:
Code:
import random messages = ['Hello', 'World', 'Happy', 'Christmas'] print random.choice(messages) |
|
#5
|
|||
|
|||
|
Even better.
I knew of random.shuffle(list), but not .choice. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Random |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|