Jump to content

[SOLVED] unique number generator - problem with variables as array pointer in while loop


logarkh

Recommended Posts

Okay I'm trying the make a script that creates a 15 random but unique numbers between 11 and 99 and puts them in an array, once the script has created a random number it will get the korean words for those numbers and put them in another array so I'd end up with something like

 

num[1] = 36

ans[1]  = sorunyosot

num[2] = 12

ans[2]  = yoldool

 

hana and yoldool being the korean for 36 and 12 respectfully.

 

What the script actually does is create 15 random numbers and their korean, but they're not always unique. I'm currently getting this error:

Notice: Undefined offset: 0 in /home/vhosts/quizsamples.atbhost.com/public_html/num_array.php on line 21

which refers to the while statement. So what I'm thinking is that I'm incorrectly pointing to the variables or something like that. Here's the code that I have

<?php
$arr = array("units" => array(1 => "hana", "dool", "set", "net", "tasot", "yosot", "ilgop", 

"yodol", "ahop"), "tens" => array(10 => "yol", 20 => "samul", 30 => "sorun", 40 => "mahun", 50 => 

"chiun", 60 => "yesun", 70 => "irrun", 80 => "yodun", 90 => "ahun"), "hundreds" => array(100 => 

"pek"));
$ans = array(0 => 0);
$a;
$b;
$c;

for($a = 1; $a < 16; $a = $a + 1) {
$var1 = rand(1,9)*10;
$var2 = rand(1,9);
$num = array($a => $var1 + $var2);
$ans = array($a => $arr["tens"][$var1].$arr["units"][$var2]);
for($b = 0; $b < $a - 1; $b = $b + 1) {
	while($num[$a] == $num[$b]) {
		$var1 = rand(1,9)*10;
		$var2 = rand(1,9);
		$num = array($a => $var1 + $var2);
	}
}
echo $num[$a].'<br />'.$ans[$a].'<br />';
}

?>

 

Any ideas on how to correct it? :)

Link to comment
Share on other sites

It is refering to the $num[$b] portion.

 

What essentially is happening is there is no value of $num[$b] set in the $num array. Since that is an invalid index of that array the notice gets thrown telling you so. A way to avoid it is using the www.php.net/isset function. If you do not want to generally in production notices are turned off anyway.

Link to comment
Share on other sites

Based upon what you said, I think you are making it a little harder than it needs to be. The following 5 lines of code (with comments removed) will generate an array with 15 random numbers from 11 to 99 - and they will be unique.

 

<?php
// Create empty array variable
$num = array();

// Create a loop that will continue as long
// as the array has less than 15 items
while (count($num)<15) {

   // Generate rand number from 11 to 99
   $randVar = rand(11,99);

   // If the rand number does not
   // exist in the array add it
   if (!in_array($randVar,$num)) { $num[] = $randVar; }
}
?>

Link to comment
Share on other sites

Ahh, thank you. I realised it was a complicated way but that's the only method I could come up with based on what I remember about php, it has been many years since I last used it. That script hopefully sorts out my problem, I just need to add the other stuff to it then I'll let you know and mark this as solved.

Link to comment
Share on other sites

Sorry I had to go to an interview, yeh I need it that way so that I can call the word for the tens number and the word for the units number from an array, unless there's another way to do that? Otherwise this is solved.

Link to comment
Share on other sites

Ok, I didn't get that in your first post. I'd still use the same logic, but with some modifications.

 

I think this is a but cleaner:

 

<?php

$arr = array(
  "units"    => array(1 => "hana", "dool", "set", "net", "tasot", "yosot", "ilgop", "yodol", "ahop"),
  "tens"     => array(10 => "yol", 20 => "samul", 30 => "sorun", 40 => "mahun", 50 => "chiun", 60 => "yesun", 70 => "irrun", 80 => "yodun", 90 => "ahun"),
  "hundreds" => array(100 => "pek")
);

// Create empty array variable
$num = array();

// Create a loop that will continue as long
// as the array has less than 15 items
while (count($num)<15) {

   // Generate rand numbers for ones and tens
   $units = rand(1,9);
   $tens  = rand(1,9) * 10;
   $randVar = $tens + $units;

   // If the rand number does not
   // exist in the array add it
   if (!in_array($randVar,$num)) { $num[] = $randVar; }
}

//Extract & print the words
foreach ($num as $value) {
   $tens_word = $arr[tens][$value-$value%10];
   $units_word = $arr[units][$value%10];
   $word = $tens_word . $units_word;
   echo $value . ": " . $word . "<br>";
}

?>

 

Or, you could reduce all of that to just a few lines of code:

<?php
$num = array();
while (count($num)<15) {
   $randVar = rand(1,9) * 10 + rand(1,9);
   if (!in_array($randVar,$num)) { $num[] = $randVar; }
}

foreach ($num as $value) {
   $word = $arr[tens][$value-$value%10] . $arr[units][$value%10];
   echo $value . ": " . $word . "<br>";
}
?>

 

by the way, what is the hundereds item in the array for?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.