473,804 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with random array

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($r ec))];
print $rec;

problem is, is sometimes it returns nothing. can anyone see how
to make this so it always selects something.

thanks.
Jul 16 '05 #1
4 6110
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($r ec))];
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($r ec)-1)];

-Sami

Jul 16 '05 #2
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($r ec))];
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($r ec))];
} while ($rec = "");

Jul 16 '05 #3
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($r ec))];
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($r ec)) 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)
Jul 16 '05 #4
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($r ec))];

should be:

$rec = $rec[rand(0,count($r ec)-1)];

Jerry


"Tyrone Slothrop" <ts@paranoids.c om> wrote in message
news:fk******** *************** *********@4ax.c om...
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($r ec))];
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($r ec))];
} while ($rec = "");

Jul 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
3724
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. The task was to use PHP to generate a random bingo card. The standard UK card has nine rows and three columns. Each row has five numbers. All numbers are different, out of a pool of 90. I asked my student to design one card. He came back a...
4
1251
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
6
10268
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 off. im getting a recurring error regarding the syntax of my main() function argument calling in lottery_numbers array. so any help as to whats glaringly going wrong here?? oh and some brief guidance on whats the easiest way to sort the numbers...
2
1319
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 3 or 4 times, but after that, it gets collapsed and i have to close the window. Any advice? <script language="javascript"> // numero de ejercicios que componen todo. all the exercises var numero_de_ejercicios=10;
2
1785
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 first game, but then all the other games have exactly the same numbers (e.g. Game 1: 1,2,3,4,5,6.. Game 2: 1,2.3,4,5,6). I don't know how to get delete the previous array with those numbers, and then generate a new one. Here is my code:
2
1243
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 binary search on a word list to see if that string is listed. However, at seemingly random intervals (I've seen it happen on everything from a few dozen calls to the function to a few thousand) I get a system.stackoverflowexception. I'm assuming I've...
3
1648
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 7 1 6 99 8 9 8 3 5 3 12 9 4 2 2 15
3
1382
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 http://www.hypergurl.com/easyhtml.html for this script and many more */ // This part preloads images. You can use different images. // Change URL path to the snow image below grphcs=new Array(6) Image0=new Image(); Image0.src=grphcs="snow1.gif";
1
2107
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 the description: The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life points, a name, and a bonus factor that is used in special occasions. In this initial phase of the...
11
3024
TTCEric
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 of values. I get a random index value for each array so I can pull the data from them.
0
9714
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10600
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.