473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Random Number Not in Sequence

mshmyob
904 Expert 512MB
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)
Jan 20 '08 #1
7 2106
ADezii
8,834 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. 'Dimension an Array = the number of Elements
  2. Dim aintRndNums(1 To 4) As Integer, intRandomIndex As Integer
  3.  
  4. 'Assign the numbers to specific Indexes
  5. aintRndNums(1) = 1
  6. aintRndNums(2) = 3
  7. aintRndNums(3) = 7
  8. aintRndNums(4) = 9
  9.  
  10. Randomize        'seed the Random Number Generator
  11.  
  12. 'Grab a Random Index between 1 and 4
  13. intRandomIndex = Int(Rnd * UBound(aintRndNums()) + 1)
  14.  
  15. 'And your Random Number is (1, 3, 7, or 9)
  16. Debug.Print "Random Number: " & aintRndNums(intRandomIndex)
  17.  
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Random Number: 7
  2. Random Number: 3
  3. Random Number: 9
  4. Random Number: 3
  5. Random Number: 1
  6. Random Number: 7
  7. Random Number: 7
  8. Random Number: 9
  9. Random Number: 3
  10. Random Number: 1
  11. Random Number: 9
  12. Random Number: 1
  13. Random Number: 9
  14. Random Number: 1
  15. Random Number: 3
  16. Random Number: 1
  17. Random Number: 7
  18. Random Number: 3
  19. Random Number: 1
  20. Random Number: 9
  21. Random Number: 1
  22. Random Number: 1
  23. Random Number: 3
  24. Random Number: 1
  25. 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.
Jan 20 '08 #2
mshmyob
904 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. 'Dimension an Array = the number of Elements
  2. Dim aintRndNums(1 To 4) As Integer, intRandomIndex As Integer
  3.  
  4. 'Assign the numbers to specific Indexes
  5. aintRndNums(1) = 1
  6. aintRndNums(2) = 3
  7. aintRndNums(3) = 7
  8. aintRndNums(4) = 9
  9.  
  10. Randomize        'seed the Random Number Generator
  11.  
  12. 'Grab a Random Index between 1 and 4
  13. intRandomIndex = Int(Rnd * UBound(aintRndNums()) + 1)
  14.  
  15. 'And your Random Number is (1, 3, 7, or 9)
  16. Debug.Print "Random Number: " & aintRndNums(intRandomIndex)
  17.  
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Random Number: 7
  2. Random Number: 3
  3. Random Number: 9
  4. Random Number: 3
  5. Random Number: 1
  6. Random Number: 7
  7. Random Number: 7
  8. Random Number: 9
  9. Random Number: 3
  10. Random Number: 1
  11. Random Number: 9
  12. Random Number: 1
  13. Random Number: 9
  14. Random Number: 1
  15. Random Number: 3
  16. Random Number: 1
  17. Random Number: 7
  18. Random Number: 3
  19. Random Number: 1
  20. Random Number: 9
  21. Random Number: 1
  22. Random Number: 1
  23. Random Number: 3
  24. Random Number: 1
  25. 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.
Jan 20 '08 #3
ADezii
8,834 Expert 8TB
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.
Jan 20 '08 #4
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.
Jan 21 '08 #5
mshmyob
904 Expert 512MB
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.
Jan 23 '08 #6
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.
Jan 23 '08 #7
mshmyob
904 Expert 512MB
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.
Jan 23 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

23
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...
10
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...
3
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...
25
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; }
23
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...
4
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...
104
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...
12
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...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
26
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());...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
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,...

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.