Ok, so i have a php random number generator that creates two unique numbers between 1 and 10, what i want to add that i can't figure out for the life of me is how to implement that these two numbers will always have a separation of 4. For example, 1 and 6...or even 2 and 9.... but never 2 and 3... or 4 and 7.
<?php
$arr=array();
while(count($ar r)<2){
$x=mt_rand(1,10 );
if (!in_array($x,$ arr)){
$arr[]=$x;
echo "$x<br>";
}
}
?>
Thanks in advance.
3 1439
bump... please... any help is appreciated
code green 1,726
Recognized Expert Top Contributor
[I they must have a seperation of four, then you can only really have one random number.
So generate one random number then add or subtract four to generate the second.
If you need to randomise whether adding or subtracting then generate another rand()
between -1 and +1 and multiply the second number by this, but eliminate zeroes.
Motoma 3,237
Recognized Expert Specialist
Essentially, you are only working with 1 random number, and one complimentary number which follows the rule that it is 4 more than the random number, and less than 10. Therefore, your random number is constrained to a minimum of 1 and a maximum of 6 (10 - 4). -
<?php
-
-
$x = mt_rand(1,6);
-
$arr = array($x, $x+6);
-
-
?>
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Aaron |
last post by:
I need some help writing this function
the function returns a list of random non-duplicate integers in a string, 1
parameter indicating the maximum range.
string randGen(int maxRange)
maxRange=1000
the output would be:
|
by: Joe |
last post by:
Hi, I have been working on some code that requires a high use of random
numbers within. Mostly I either have to either:
1) flip a coin i.e. 0 or 1, or
2) generate a double between 0 and 1.
I have utilised the following random number source code
http://www.agner.org/random/
What I have found is that there is a problem with seeding. The code
generates a seed based on time(0). I have found that I need to increment
|
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 using rand() if you want more than
approx. UINT_MAX total different sequences, and so on.
So I...
|
by: Peteroid |
last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers.
But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0
with resolution of a double (i.e., every possible double value in the range
could come up with equal probability). I'd also like to be able to seed this
generator (e.g., via the clock) so that the same sequence of random values
don't come up every time.
Anybody have an easy and fast...
|
by: Wally |
last post by:
I am after VB.net code for a random number generator that will generate 10
different numbers between 1 and 10 and put them into an array. For example I
want the output to be something like 10,7,8,6,9,4,5,2,3,1.
Can anyone provide me with the code?
Thanks,
| |
by: L33VaNcL33F |
last post by:
I like to make a javascript that generate random number from the range number within : (10016486 and 99999985).
the number always + 22423 that begin from 10016486 and end at 99999985
Example random result from generate button :
10016486 + 22423 = 10038909
10038909 + 22423 = 10061332 and so on, maximum to 99999985
or if somebody can convert below c++ code to working javascript/html random number generator.
#include <cstdlib>
#include...
|
by: jason.cipriani |
last post by:
I am looking for a random number generator implementation with the
following requirements:
- Thread-safe, re-entrant.
- Produces consistently reproducible sequences of psuedo-random
numbers given a seed.
- Relatively uniform, does not have to be perfect.
The application is not a security or statistics application, the
quality of numbers is not a priority although a fairly uniform
|
by: Lexicon |
last post by:
Hello;
Does anyone know a 64-bit integer generator for 32-bit machines that
use long long or __int64 etc ?
|
by: AccessQuestion |
last post by:
So just a quick question; more out of curiosity than anything. I have a form in which I pop a random number into a text box. Eventually that number will be placed by a user using a scale, but I wanted to simulate that now. Anyway, I used this code:
MyWeight = Int(1000 * Rnd())
I assumed that will give me a random number, however EVERY time I launch my form the random numbers are the same. First 'random number' is always 705, followed...
|
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...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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();...
|
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...
|
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
| |
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...
| |