def main():
x = [randint(1,100) for i in range(1,100)]
return x
这将返回100个随机数bt 1和100.每次调用该函数时,它都会返回不同的数字序列。我想要的是每次都得到相同的数字序列。也许将结果保存到某事上?
答案 0 :(得分:7)
你可以提供一些固定的种子。
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "abtech.waiteriano.com.waitrer"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile project(':jtds-1.3.1')
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.roughike:bottom-bar:1.3.9'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
testCompile 'junit:junit:4.12'
}
有关种子的更多信息:random.seed(): What does it do?
答案 1 :(得分:5)
以下是代码
后的基本示例import random
s = random.getstate()
print([random.randint(1,100) for i in range(10)])
random.setstate(s)
print([random.randint(1,100) for i in range(10)])
在两次调用中,您获得相同的输出。关键是,您可以随时检索并稍后重新分配rng的当前状态。
答案 2 :(得分:1)
In [19]: for i in range(10):
...: random.seed(10)
...: print [random.randint(1, 100) for j in range(5)]
...:
...:
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
必须在对随机的新调用之前调用random.seed
函数。
In [20]: random.seed(10)
In [21]: for i in range(10):
...: print [random.randint(1,10) for j in range(10)]
...:
[5, 6, 3, 9, 9, 7, 2, 6, 4, 3]
[10, 10, 1, 9, 7, 4, 3, 7, 5, 7]
[7, 2, 8, 10, 10, 7, 1, 1, 2, 10]
[4, 4, 9, 4, 6, 5, 1, 6, 9, 2]
[3, 5, 1, 5, 9, 7, 6, 9, 2, 6]
[4, 7, 2, 8, 1, 2, 9, 10, 5, 5]
[3, 3, 7, 2, 2, 5, 2, 7, 9, 8]
[5, 4, 5, 1, 8, 4, 4, 1, 5, 6]
[4, 9, 7, 3, 6, 10, 6, 7, 1, 5]
[5, 5, 5, 6, 6, 5, 2, 5, 10, 5]