hello all.
I am using this code to randomly select one value from an array.
srand ();
$rec = array("1","2","3","4","5","6");
$rec = $rec[rand(0,count($rec))];
print $rec;
problem is, is sometimes it returns nothing. can anyone see how
to make this so it always selects something.
thanks. 4 6090
noone wrote: hello all. I am using this code to randomly select one value from an array.
srand (); $rec = array("1","2","3","4","5","6"); $rec = $rec[rand(0,count($rec))]; print $rec;
problem is, is sometimes it returns nothing. can anyone see how to make this so it always selects something.
thanks.
Try $rec = $rec[rand(0,count($rec)-1)];
-Sami
On Fri, 27 Jun 2003 16:25:52 -0800, "noone" <no***@nowhere.com> wrote: hello all. I am using this code to randomly select one value from an array.
srand (); $rec = array("1","2","3","4","5","6"); $rec = $rec[rand(0,count($rec))]; print $rec;
problem is, is sometimes it returns nothing. can anyone see how to make this so it always selects something.
thanks.
How about a do... while statement?
do {
$rec = $rec[rand(0,count($rec))];
} while ($rec = "");
On Fri, 27 Jun 2003 16:25:52 -0800, "noone" <no***@nowhere.com> wrote: hello all. I am using this code to randomly select one value from an array.
srand (); $rec = array("1","2","3","4","5","6"); $rec = $rec[rand(0,count($rec))]; print $rec;
problem is, is sometimes it returns nothing. can anyone see how to make this so it always selects something.
count($rec) returns 6 because there are 6 elements
rand(0,count($rec)) starts from 0 to count($rec) ie 6
0,1,2,3,4,5,6 is 7 elements.. and you only have 6 :)
--
KIPAX ( www.kipax.com)
Actually, the problem is that you are asking for a random number between 0
and 6. That's 7 possibilities. So one in seven times will reference an
array element that does not exist.
$rec = $rec[rand(0,count($rec))];
should be:
$rec = $rec[rand(0,count($rec)-1)];
Jerry
"Tyrone Slothrop" <ts@paranoids.com> wrote in message
news:fk********************************@4ax.com... On Fri, 27 Jun 2003 16:25:52 -0800, "noone" <no***@nowhere.com> wrote:
hello all. I am using this code to randomly select one value from an array.
srand (); $rec = array("1","2","3","4","5","6"); $rec = $rec[rand(0,count($rec))]; print $rec;
problem is, is sometimes it returns nothing. can anyone see how to make this so it always selects something.
thanks.
How about a do... while statement?
do { $rec = $rec[rand(0,count($rec))]; } while ($rec = ""); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Geoff Berrow |
last post by:
I may have mentioned that I run an Introduction to PHP course at a local
college (very basic - I'm no PHP expert). Well, one of my students was
doing really well so I set him some extension work. ...
|
by: Carl |
last post by:
I have the following matrix that I want to transform:
import random
import Numeric as N
ru =
for i in range(25):
ru.append(int(random.uniform(0, 2)))
ru = N.reshape(ru, (5, 5))
>>> ru
|
by: moi |
last post by:
hi, i have an assignment to put 6 pseudo random numbers into an array to
simulate drawing 6 lottery numbers between 1-49. the code i have to do this
so far is listed below. i dont think its far...
|
by: |
last post by:
hello,
I have a problem with this piece of code. I want to open a random web page
each time and remember which one has been opened before because I donīt want
to repeat. It works fine the first...
|
by: jerryau |
last post by:
Hi,
I'm creating a number generator program, that is supposed to generate
6 unique random numbers for each game. I want to generate this for 6
games. The problem is, that it works for the...
|
by: Michael Sutherland |
last post by:
Here's an interesting problem I've come across.
I'm writing a program that essentially generates random strings (its a
simulator of the game Boggle) and sends them to a function that does a...
|
by: RadiationX |
last post by:
Hello, I have a simple print output questioin. What I'm trying to do is
print an array of lenth 20 which holds 20 ints in the form of five
columns and four rows. like this below
12 80 90 6 ...
|
by: blogger |
last post by:
i try use this script but keep getting
Char: 6
Error: Expected ';'
What can i do?
<SCRIPT
type="text/javascript"> /*Script distributed by Hypergurl.com Visit ...
|
by: javabeginner123 |
last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
|
by: TTCEric |
last post by:
This will be original. I promise.
I cannot get the random number generator to work.
I tried seeding with Date.Now.Milliseconds, it still results in the same values.
What I have are arrays...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |