473,320 Members | 1,990 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,320 software developers and data experts.

How to create a random number x times and only show it once

I have the random number generator that works but the some numbers repeat. I want to create 26 numbers between 1 and 26 and each number can only be used once.

Expand|Select|Wrap|Line Numbers
  1.     srand((unsigned)time(0)); 
  2.     int randNum=1; 
  3.     for(int amtNum=0; amtNum<26; amtNum++)
  4.     { 
  5.  
  6.     randNum = (rand()%26)+1;  // makes random number from 1 to 26
  7.  
  8.     cout << randNum << endl;
  9.  
Oct 9 '10 #1
3 1923
donbock
2,426 Expert 2GB
Perhaps the simplest approach is to keep track of the numbers you already have. Each time you get a new random number check to see if it is one you already have. If it is, then discard it and loop back and try again.

Somewhat more complicated would be to start with a pool of 26 numbers. Pick a 1-in-26 random value and use it as an index into the pool. You now are left with a pool of 25 values. Pick a 1-in-25 random value. Repeat until you're done. No need to do a final pick when the pool has only one value in it.

This more complicated approach will complete in precisely 26 equal-size steps. The simpler approach will take progressively longer -- you will find yourself spending more and more time discarding repeated values.
Oct 9 '10 #2
ADezii
8,834 Expert 8TB
I'm on the other side of town in the Access Forum, and I accidentally came across this Thread. I created an Algorithm many years ago in Visual Basic that will accomplish exactly what you are requesting. It is portable, and should easily be converted to any language (C in this case). The General Logic is as follows:
  1. Generate a Random Number of Values as defined by the CONSTANT conNUM_OF_VALUES (in your case, 26).
  2. Populate an Array with these Randomly Generated Values between 1 and conNUM_OF_VALUES.
  3. Iterate through this Array via 2 Nested Loops checking every Value in the Array against itself, except for equivalent Indexes.
  4. Repeat this process as long as Duplicate(s) exist.
  5. Output the results to the Immediate Window.
  6. I normally do not like the idea of using Unconditional Branching with GoTo outside of Error Trapping, but it seems to work well in this case. This Logic can easily be replace with a variation of the Do...Loop Structure using a Boolean Variable to check for Duplication.
  7. The Logic seems to work quite well with a small number of Randoms, but has never been tested with a larger amount.
    Expand|Select|Wrap|Line Numbers
    1. 'Array to hold Randomly Generated Numbers
    2. Dim aintRndNums() As Integer
    3. Const conNUM_OF_VALUES As Integer = 26      'Assume Ranges starts at 1 (1 to 26)
    4. ReDim aintRndNums(1 To conNUM_OF_VALUES)    'Redimension appropriately
    5. Dim intCtr As Integer                       'Counter for Loops
    6. Dim intCtr_2 As Integer                     'Counter for Loops
    7.  
    8. Randomize   'Seed the Random Number Generator
    9.  
    10. 'Poppulate Array with conNUM_OF_VALUES Randomly Generated Numbers
    11. 'between 1 and conNUM_OF_VALUES
    12. For intCtr = 1 To conNUM_OF_VALUES
    13.   aintRndNums(intCtr) = Int(Rnd() * conNUM_OF_VALUES + 1)
    14. Next intCtr
    15.  
    16. 'Lets eliminate the Duplicates
    17. DoItAllOverAgain:
    18. For intCtr = 1 To UBound(aintRndNums)
    19.   For intCtr_2 = 1 To UBound(aintRndNums)
    20.     If intCtr <> intCtr_2 Then      'Check each Element against every other
    21.                                     'Element except itself for Dups
    22.       If aintRndNums(intCtr) = aintRndNums(intCtr_2) Then    'a Dup, get a New Random
    23.         aintRndNums(intCtr) = Int(Rnd() * conNUM_OF_VALUES + 1)
    24.           GoTo DoItAllOverAgain
    25.       End If
    26.     End If
    27.   Next intCtr_2
    28. Next intCtr
    29.  
    30. 'For Verification purposes
    31. For intCtr = 1 To conNUM_OF_VALUES
    32.   Debug.Print aintRndNums(intCtr)
    33. Next intCtr
  8. OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1.  10 
    2.  23 
    3.  1 
    4.  4 
    5.  20 
    6.  25 
    7.  15 
    8.  22 
    9.  18 
    10.  14 
    11.  8 
    12.  9 
    13.  21 
    14.  11 
    15.  5 
    16.  12 
    17.  19 
    18.  17 
    19.  2 
    20.  24 
    21.  26 
    22.  7 
    23.  13 
    24.  6 
    25.  16 
    26.  3 
Oct 9 '10 #3
Well, what i did was to create a bool array and check every time if number was used in a while statement.
Check bellow.
____________________________________________

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. const int MAX_NUM(26);
  7.  
  8. class RANDOM_NUM{
  9. public:
  10.     void CreateRandNum();
  11.     void SetNumsUnused();
  12.     bool CheckIfAllUsed();
  13.     int GetRandNum();
  14. private:
  15.     bool numberUsed[MAX_NUM];
  16.     int randNum;
  17. };
  18.  
  19. void RANDOM_NUM::CreateRandNum()
  20. {
  21.         bool numIsUsed(true); 
  22.  
  23.         do{
  24.             randNum = (rand()%26)+1; 
  25.             if(!numberUsed[randNum-1]) // if number is not used exit statement  and set it to used.
  26.             {
  27.                 numberUsed[randNum-1] = true;
  28.                 numIsUsed=false;
  29.             }
  30.  
  31.         }while(numIsUsed);
  32. }
  33.  
  34. bool RANDOM_NUM::CheckIfAllUsed()
  35. {
  36.         for(int i=0;i<MAX_NUM;i++)
  37.         {
  38.             if(!numberUsed[i]) // if a number is not used it returns false.
  39.                 return false;
  40.  
  41.         }
  42.  
  43.         return true;
  44. }
  45.  
  46. void RANDOM_NUM::SetNumsUnused()
  47. {
  48.     for(int i=0;i<MAX_NUM;i++)
  49.         numberUsed[i] = false;
  50. }
  51.  
  52. int RANDOM_NUM::GetRandNum()
  53. {
  54.     return randNum;
  55. }
  56.  
  57. int main(int argc,char *argv)
  58. {
  59.      srand((unsigned)time(0)); 
  60.     RANDOM_NUM randomNum;
  61.     randomNum.SetNumsUnused();
  62.  
  63.     while(!randomNum.CheckIfAllUsed())
  64.     {
  65.         randomNum.CreateRandNum();
  66.         cout << "Random Number: " << randomNum.GetRandNum() << endl;
  67.     }
  68.     getchar();
  69.     return 0;
  70. }
I just wrote and test the above code and it works. I hope it helps ;)
oh and if u are using pure C change cout to printf and class to structure XD, i think the other stay the same ? Anyone correct me if I am wrong..
Oct 11 '10 #4

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

Similar topics

4
by: Wahoo | last post by:
Another question,my teacher gave me a code for generate a random number from 1 - range, but I can't made it work, where is the problem? Thanks!!
12
by: xeys_00 | last post by:
#include<iostream> #include<cstdlib> //This program will be a number guessing game int main() { //variables for the game
6
by: Starbuck | last post by:
Hi In VB6 we used the following to create a unique random number - Function longSerial() As Long longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer * 100), "0000000"))...
3
by: Mr. Ken | last post by:
Sorry for the non-C++ topic, but it's most easy to get answer from here I guess. How do I create random numbers (integer from 0~9999) in Unix C-shell? I am writing a script which randomly...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
5
by: Christo | last post by:
hello I am new to java very new and am hoping someone can offer me some help. I know that i need to use java.util.Random somewhere to generate a random number can anyone show me the code to...
7
by: bipi | last post by:
Dear all, I found function rand(), it can create random number but this function can not define the range of number which I want to get it, such as, I want to get random number in the range from...
2
by: wisten | last post by:
This program is to create a simple application to generate random number between 1 and 50. additional: when the user click on the Start button, a random number will be displayed every 300ms.Display...
3
by: d927 | last post by:
How can i make it so a form will only show the very first time the program is run as a whole?
2
by: jc1328 | last post by:
Hello, I'm using the random generator with 20 numbers. rand() %20+1. Is there a way that the random number can only show up once, so I can go through all 20 numbers before it displays the same...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.