Heim > Fragen und Antworten > Hauptteil
PHPz2017-04-10 15:03:18
inspired by @finallygo
$range = range(1,100);
1,
shuffle($range);
$first = $range[0];
$second = $range[1];
2,
list($first, $second) = array_rand($range,2);
//array_rand return index which starts from 0
$first++; $second++;
PHP中文网2017-04-10 15:03:18
楼主应该是系统用php实现吧(以下代码需要在php 5.3以上版本支持)
1 <?php
2 $a = new SplDoublyLinkedList();
3 for($i=1;$i<=100;$i++) {
4 $a->push($i);
5 }
6 $fetch_size = 2;
7 for($j=0;$j<$fetch_size;$j++) {
8 $index = rand() % count($a);
9 echo 'rand(' . $j . '):' . $a->offsetGet($index) . "\n";
$a->offsetUnset($index);
10 }
11 ?>
巴扎黑2017-04-10 15:03:18
Random rnd = new Random();
int a, b;
a = rnd.Next(100) + 1;
b = rnd.Next(99) + 1;
if (a == b) b = 100;
Console.WriteLine("{0} {1}", a, b);
Random rnd = new Random();
var arr = Enumerable.Range(1, 100).OrderBy(t => rnd.Next()).Take(2).ToArray();
Console.WriteLine("{0} {1}", arr[0], arr[1]);
迷茫2017-04-10 15:03:18
楼上太取巧啦。还是我来个吧
cat /dev/random
http://zh.wikipedia.org/zh-cn//dev/random