如何从随机选择的值中获​​取非随机值?

时间:2015-07-22 20:47:17

标签: php arrays random variable-assignment

我正在尝试为随机选择的值选择非随机值。

  • 第一列代表销售号码(1-30)
  • 第二列显示必须随机选择的项目
  • 第三列显示所述项目的相应价格

如果随机选择该项目,我该如何实现?

我曾想过使用switch语句,但这可能会变得混乱。

$items = array( ‘clock’ , ‘kettle’ , ‘mug’ , ‘toaster’ , ‘CD’);
$prices = array( '£30', '£19', '£5' , '£14' , '£7');

for($i = 0; $i < 30; $i++) {
    $sales[$i][0] = $i + 1; //Starts at 1 and increments
    $sales[$i][1] =  $items[rand(0,4)]; //Item chosen at random
    $sales[$i][2] = //Should be the corresponding price for the above item.
}

1 个答案:

答案 0 :(得分:3)

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>sorm-test</groupId>
<artifactId>sorm</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-reflect</artifactId>
        <version>[2.10,2.12)</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>[2.10,2.12)</version>
    </dependency>

    <dependency>
        <groupId>org.sorm-framework</groupId>
        <artifactId>sorm</artifactId>
        <version>0.3.8</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.168</version>
    </dependency>

</dependencies>

<build>
    <sourceDirectory>src/main/scala</sourceDirectory>
<plugins>
<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <recompileMode>incremental</recompileMode>
        <useZincServer>true</useZincServer>
    </configuration>
    <executions>
        <execution>
            <id>compile</id>
            <goals>
                <goal>compile</goal>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
</plugins>
</build>

观察并学习:

$items = array( ‘clock’ , ‘kettle’ , ‘mug’ , ‘toaster’ , ‘CD’);
$prices = array( '£30', '£19', '£5' , '£14' , '£7');

for($i = 0; $i < 30; $i++) {
    $sales[$i][0] = $i + 1; //Starts at 1 and increments
    $sales[$i][1] =  $items[rand(0,4)]; //Item chosen at random
    $sales[$i][2] = //Should be the corresponding price for the above item
}