Use the shortest code to generate two unequal random numbers

Source: Internet
Author: User
Use 1 to 100 as the fixed interval to generate two numbers. Do not use recursion. Use 1 to 100 as the fixed interval.

Generate two numbers without Recursion

Reply content:

Fixed range from 1 to 100

Generate two numbers without Recursion

Specified red 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 syntax does not understand, but it can be converted into two random numbers, two subtract as the first number, and the sum as the second number

The main idea is to use php (the following code must be supported in php 5.3 or later)

  1 
  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 ?>

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]);

var a, b; a = Math.random(); do{ b = Math.random(); } while(b == a)

Any series can be called "random series". The difference is that the probability distribution is different. Since the subject does not mention the distribution, I can only assume that the subject does not care:
$a=1; $b=2;
It fully meets the subject requirements.

It's so clever to go upstairs. Let me do it.

cat /dev/random

Http://zh.wikipedia.org/zh-cn//dev/random

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.