473,670 Members | 2,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random subarray from an array

Hi, everybody

First, the code:
---------------------
$maxNo = 500
$numbers = range (1,$maxNo);
srand ((double)microt ime()*1000000);
shuffle ($numbers);
$subarray = array_slice ($numbers, 0, 24);
ksort ($subarray); // this also don't work!!!
while (list (, $number) = each ($subarray)) {
echo "$number, ";
}
---------------------

I hoped this piece of code will generate random numbers from 1 to 500
but every time it generates the same sequence. Why?

Also, when I change $maxNo to some other number, it changes the
subarray, but then it stayes the same until I change the $maxNo again.
If I change it back to 500, it's the first sequence all over again.
This is the infamous sequence:
30, 3, 482, 286, 226, 414, 158, 258, 98, 478, 222, 354, 94, 386, 130,
350, 34, 481, 225, 290, 97, 418, 162, 353,

This is definitely not how I understand word "random"!

And one more thing: this example is how this thing works on my home
computer. I have the same application on web server, and it doesn't
work the same way! It changes subarray every time I refresh the page,
but the numbers are inclined to upper limit, so if I put $maxNo = 500,
10 out of 24 numbers will be between 480 and 500. Why they prefer
upper limit?

This is very confusing for me...

Please, help

Ante
Jul 17 '05 #1
1 3799
Ante Perkovic wrote:
Hi, everybody

First, the code:
---------------------
$maxNo = 500
$numbers = range (1,$maxNo);
srand ((double)microt ime()*1000000);
try
echo (double)microti me()*1000000;

I get different values every time ... but you might not :)

According to php.net, as of php 4.2.0 you needn't initialize the random
number generator ... try leaving the srand() call out

shuffle ($numbers);
$subarray = array_slice ($numbers, 0, 24);
ksort ($subarray); // this also don't work!!!
ksort() -- sort by key ... is this what you want?
or is it a simple sort() ?

while (list (, $number) = each ($subarray)) {
echo "$number, ";
}
maybe better:
echo implode(', ', $subarray);

---------------------

but the numbers are inclined to upper limit, so if I put $maxNo = 500,
10 out of 24 numbers will be between 480 and 500. Why they prefer
upper limit?


This is strange!
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #2

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

Similar topics

16
4208
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have shelled out on VC++ 6.0 compiling that same code is proving difficult. I am not too worried how I generate random numbers in c++, as long as it is sufficiently random. Can anybody help me out in getting what I want from VC++?
10
5971
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object System.Random iRnd = new System.Random(); string theNum = iRnd.Next(0,8).ToString(); lblAnswer.Text = iRnd.Next(0,8).ToString();
1
6315
by: Need Helps | last post by:
Writing my own algorithm to make a subarray in Visual C++ would be easy enough, but is there a useful function or method for doing this? I have strings stored as TCHAR and I want to get substrings of those, but the basic String.substr() function doesn't compile on that. I tried this TCHAR string TCHAR substring //function that returns a value for strin substring = string->Substring(3,3) and I get this when I compile error C2227: left...
1
6066
by: artev | last post by:
if I have array-n-01= array-n-02= want change in one array03 so: array03= array03= array03= array03=
6
2123
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and refilled at each turn of cycle. On my developing pc, the same sequence of random numbers was repeated from on turn of cycle to the other; I put some Application.DoEvents() and all gone well.
2
2030
by: gm04030276 | last post by:
hey, i'm basically trying to use php sessions and a bit of javascript to make tabbed browsing within a web page using sessions to store the data of the different opened pages. Problem: when i unset the first 0 index in the array (ie close the default page) it seems to wipe the other one (can't get it to add more that one anymore) from the array also the information is held in sub array $_SESSION each page has its own array of infomation...
5
4535
oll3i
by: oll3i | last post by:
why when i try to use subarray (for byte array)function eclipse says it's undefined but i read that this function is defined in java? Thank YOU
11
3017
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.
4
3693
by: puzzlecracker | last post by:
say I have an array string array1={"aa","bb"...}; I want to create second array that will contain all elements first array except for the very first one ? Thanks
0
8471
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
8815
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
8592
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
5686
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4213
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
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2802
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
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
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.