I know how to create a random number using lower and upper limits but am curious if you can generate a random number from a set of specific numbers.
For instance I want to randomly pick either 1, 3, 7, or 9 only.
Right now I just put the random number generator with a lower limit of 1 and an upper limit of 9 in a loop until it generates one of the above numbers.
I was wondering if there was a simple formula like (this is the standard upper limit lower limit formula)
vLoop = Int((9 - 1 + 1) * Rnd + 1)
7 2106
I know how to create a random number using lower and upper limits but am curious if you can generate a random number from a set of specific numbers.
For instance I want to randomly pick either 1, 3, 7, or 9 only.
Right now I just put the random number generator with a lower limit of 1 and an upper limit of 9 in a loop until it generates one of the above numbers.
I was wondering if there was a simple formula like (this is the standard upper limit lower limit formula)
vLoop = Int((9 - 1 + 1) * Rnd + 1)
The following code will generate a Random 1, 3, 7, or 9. I also list a sample display of 25 Random Generations for the 4 specific numbers. Hope this helps: -
'Dimension an Array = the number of Elements
-
Dim aintRndNums(1 To 4) As Integer, intRandomIndex As Integer
-
-
'Assign the numbers to specific Indexes
-
aintRndNums(1) = 1
-
aintRndNums(2) = 3
-
aintRndNums(3) = 7
-
aintRndNums(4) = 9
-
-
Randomize 'seed the Random Number Generator
-
-
'Grab a Random Index between 1 and 4
-
intRandomIndex = Int(Rnd * UBound(aintRndNums()) + 1)
-
-
'And your Random Number is (1, 3, 7, or 9)
-
Debug.Print "Random Number: " & aintRndNums(intRandomIndex)
-
Sample OUTPUT: - Random Number: 7
-
Random Number: 3
-
Random Number: 9
-
Random Number: 3
-
Random Number: 1
-
Random Number: 7
-
Random Number: 7
-
Random Number: 9
-
Random Number: 3
-
Random Number: 1
-
Random Number: 9
-
Random Number: 1
-
Random Number: 9
-
Random Number: 1
-
Random Number: 3
-
Random Number: 1
-
Random Number: 7
-
Random Number: 3
-
Random Number: 1
-
Random Number: 9
-
Random Number: 1
-
Random Number: 1
-
Random Number: 3
-
Random Number: 1
-
Random Number: 7
P.S. - All the above boils down to: sequentially placing the numbers you wish to choose at Random into an Array, selecting a Random Index into the Array, and listing the value at that Index.
Very interesting way of doing it. With a minor change I can dynamically modify my random numbers.
The following code will generate a Random 1, 3, 7, or 9. I also list a sample display of 25 Random Generations for the 4 specific numbers. Hope this helps: -
'Dimension an Array = the number of Elements
-
Dim aintRndNums(1 To 4) As Integer, intRandomIndex As Integer
-
-
'Assign the numbers to specific Indexes
-
aintRndNums(1) = 1
-
aintRndNums(2) = 3
-
aintRndNums(3) = 7
-
aintRndNums(4) = 9
-
-
Randomize 'seed the Random Number Generator
-
-
'Grab a Random Index between 1 and 4
-
intRandomIndex = Int(Rnd * UBound(aintRndNums()) + 1)
-
-
'And your Random Number is (1, 3, 7, or 9)
-
Debug.Print "Random Number: " & aintRndNums(intRandomIndex)
-
Sample OUTPUT: - Random Number: 7
-
Random Number: 3
-
Random Number: 9
-
Random Number: 3
-
Random Number: 1
-
Random Number: 7
-
Random Number: 7
-
Random Number: 9
-
Random Number: 3
-
Random Number: 1
-
Random Number: 9
-
Random Number: 1
-
Random Number: 9
-
Random Number: 1
-
Random Number: 3
-
Random Number: 1
-
Random Number: 7
-
Random Number: 3
-
Random Number: 1
-
Random Number: 9
-
Random Number: 1
-
Random Number: 1
-
Random Number: 3
-
Random Number: 1
-
Random Number: 7
P.S. - All the above boils down to: sequentially placing the numbers you wish to choose at Random into an Array, selecting a Random Index into the Array, and listing the value at that Index.
Very interesting way of doing it. With a minor change I can dynamically modify my random numbers.
It is very adaptable, the numbers you select for Random Generation can be entered into a Table, you can enter as many as you like, and the code will do the rest with slight modification. This simple demo has you manually entering the valuers into an Array, this need not be the case.
NeoPa 32,556
Expert Mod 16PB
In fact, any definable way of mapping the numbers can be used. In your case for instance, you could get a random number between 1 & 4, then multiply by 2 and subtract 1.
If there's a formula for it, it can be done.
I like the way Dezi did it. Storing previous results or new numbers in a table for future reference is something I may be able to use.
As you may have seen in my previous posts I am creating numerous learning type games (math, speling etc.) for my 7 yr old. And I also wanted to just create a simple game for her so I created a Tic Tac Toe game. So it is my first AI type program. It worked out very well and I put in 3 levels of difficulty.
I am now expanding the Tic Tac Toe game to a connect 4 type game but it allows the user to generate any size grid .
I think with the way Dezi did it I can create a system that allows the computer to actually learn.
NeoPa 32,556
Expert Mod 16PB
I like the way Dezi did it. ...
So do I. So do I.
I was merely expanding on the possibilities of the concept. Had ADezii suggested the algorithmic approach, I would have mentioned the array one. Each has its pros and cons. The array approach is generally more flexible but can take more resource. Nowadays that's barely an issue of course.
I understood what you were pointing out and I did appreciate it. Your way would also work for me and I see that both have merits and are useful in different situations.
I like to see the different ways people come up with a solution to a problem, it gets me thinking on different paths.
For instance when I created the Tic Tac Toe game I did it without checking out any other code. After I got it to work I went online and checked how other people did it and found I did it in a very unique way. Almost all people used a 2 dimensional array but I used a 1 dimensional array and assigned each column, row and diagonal to the array. I worked out a simple formula of either adding or subtracting 1 from the number in the array (all started at 0). When a value either reached -3 or +3 that determined whether O or X won and also determined where the computer's next move should always be. But I also appreciated the 2 dimensional array method although it seemed more processor intensive (not that it would make much difference like you pointed out ).
I always appreciate everybody's input as long as they are constructive. And you guys always get me thinking (don't want the old grey matter to dry up).
So do I. So do I.
I was merely expanding on the possibilities of the concept. Had ADezii suggested the algorithmic approach, I would have mentioned the array one. Each has its pros and cons. The array approach is generally more flexible but can take more resource. Nowadays that's barely an issue of course.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Thomas Mlynarczyk |
last post by:
I remember there is a programming language where you can initialize the
random number generator, so that it can - if you want - give you the exactly
same sequence of random numbers every time you...
|
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...
|
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...
|
by: JNY |
last post by:
I am using random to generate random numbers, thus:
int x,y;
for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}
|
by: MConly |
last post by:
Can you tell me what happens inside CPU when I rand() ?
Where can I find the true rand function implemented ? I have heard
that rand() in C/C++ is n't a good one but why it isn't a good one,
are...
|
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...
|
by: fieldfallow |
last post by:
Hello all,
Is there a function in the standard C library which returns a prime
number which is also pseudo-random?
Assuming there isn't, as it appears from the docs that I have, is there
a...
|
by: Jim Michaels |
last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
I have not been able to do either. I get the same number back several times
from PHP's mt_rand() and from mysql's...
|
by: chico_yallin |
last post by:
I just wana make a random id number based on4 digits-for examples??
Thanks in Advance
Ch.Yallin
|
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());...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
| |