473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pseudo Random Sequence

Nel
Hi all,

I am driving myself crazy trying to think of a solution to this - perhaps
one of you guys can see a simple solution.

The problem is displaying the contents of an array in what appears to be
random order, but in fact is a set sequence depending on a given value (e.g.
the day of the month).

So on the 1st of the month the array is always displayed as:
T,G,Q,C,I,N,E,A

On the 2nd day you ALWAYS get
G,I,A,N,E,T,Q,N

The order itself is not important, but what is important is the apparent
random sequence is really repeatable.

Hope you can make some sense out of this.

Many thanks,

Nel.
Nov 10 '05 #1
4 1852
"Nel" wrote:
The problem is displaying the contents of an array in what appears to be
random order, but in fact is a set sequence depending on a given value (e.g.
the day of the month).
(snip)
The order itself is not important, but what is important is the apparent
random sequence is really repeatable.


Seed the random number generator based on the day of the month and use
array_rand() to shuffle the contents

$day = 1 * date('j');
srand($day);
$random_array = array_rand($arr ay);

(I haven't tested this, but it ought to work)

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/

Nov 10 '05 #2
Philip Ronan wrote:
Seed the random number generator based on the day of the month and use
array_rand() to shuffle the contents


Yep, only it's shuffle()...
Gives the same results every time:

$a = range('A', 'Z');
for ( $i = 1; $i < 32; ++$i )
{
$b = $a;
srand($i);
shuffle($b);
echo sprintf('%2d ', $i).implode(' ', $b)."\n";
}

--
E. Dronkert
Nov 10 '05 #3
Following on from Nel's message. . .
Hi all,

I am driving myself crazy trying to think of a solution to this - perhaps
one of you guys can see a simple solution.

The simple solution is to look at the documentation. What do you see
there.... srand()

--
PETER FOX Not the same since the bra business went bust
pe******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.co.uk>
Nov 10 '05 #4
Nel

"Ewoud Dronkert" <fi*******@last name.net.invali d> wrote in message
news:8k******** *************** *********@4ax.c om...
Philip Ronan wrote:
Seed the random number generator based on the day of the month and use
array_rand() to shuffle the contents


Yep, only it's shuffle()...
Gives the same results every time:

$a = range('A', 'Z');
for ( $i = 1; $i < 32; ++$i )
{
$b = $a;
srand($i);
shuffle($b);
echo sprintf('%2d ', $i).implode(' ', $b)."\n";
}

--
E. Dronkert


Thanks to you both for your advice - unless you've come across such a
feature before it's hard to know if such a thing is possible so easily.

Thanks again!!!

Nel.
Nov 10 '05 #5

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

Similar topics

10
2882
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I have is that the first loop I get a sequence of random numbers untuil I get a match, BUT then on the following loops I get the SAME random(?)...
7
3914
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of rand(): #include <cstdlib> #include <ctime> int GetRandomNumber()
1
4712
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length (nrow x ncol) and every element of the vector contians its pixel value. vector float V ;
4
5094
by: Jesse Noller | last post by:
Hello - I'm probably missing something here, but I have a problem where I am populating a list of lists like this: list1 = list2 = list3 = main_list =
4
8243
by: Greg Strong | last post by:
Hello All, Is it possible to create multiple random numbers in a query where there are numerous records? I've created a custom function. When I use it in a query it creates the same random number for ALL the records. It appears the function is only getting called once, therefore only one random number is being generated for all the...
70
6206
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not...
1
2877
by: aboood | last post by:
Hi guys Could any one tell me what does that code do please? in pseudo-code #include <fstream.h> #include <iostream.h> bool differsByOneDigit ( int , int ); void outputResults ( ofstream & , int * , int , bool );
3
2725
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a method which produces a random DNA sequence. I would like to write a unit test for it, but can't quite figure out what makes sense. It is easy to test that a DNA sequence is produced. What I can't figure is randomness. By it's very nature random is random. I have written a simple test that generates 500 sequences and checks that...
26
7880
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand()); int r2 = rand(); bool f = r1 == r2;
0
7697
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...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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. ...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
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...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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...
1
2113
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
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.