Results 1 to 2 of 2

Thread: Randomize value C++

  1. #1
    Join Date
    Feb 2011
    Posts
    1
    Platforms
    Windows

    Default Randomize value C++

    Hello,

    I'd like to ask you for help. I want to create a program which ask an user for 3 values. These values will be stored in the table. Later on the program will draw two of them, but these values can not be the equal.

    I wrote such a code but somewhere there is a mistake but unforunetaly I can not find them. I will appreciate if you could help me to find them.

    Qt Code:
    1. Witam,
    2.  
    3. Mam mały problem. Moją intencją było napisanie programu który zapisuje do tablicy 3 podane przez usera liczby a następnie losuje dwie z nich (bez powtórzeń) i wyświetla je na monitorze.
    4.  
    5. W kodzie jest coś nie tak bowiem wyświetlane są również powtórki. Będę bardzo wdzięczny za wskazanie błędu- nie mogę się go dopatrzeć!
    6.  
    7. [cpp]
    8.  
    9. #include <iostream>
    10. #include <cstdlib>
    11. #include <ctime>
    12.  
    13. using namespace std;
    14.  
    15. bool bezPowtorek (int aWylosowana, int aJ)
    16. {
    17. int tab [2];
    18.  
    19. if (aJ <1)
    20. {
    21. aWylosowana = tab[aJ];
    22. return false;
    23. }
    24.  
    25. int i=0;
    26. do
    27. {
    28. if(tab[i] == aWylosowana)
    29. return true;
    30.  
    31. i++;
    32. }while(i<aJ);
    33.  
    34. tab[aJ] = aWylosowana;
    35. return false;
    36. }
    37.  
    38. int losowanie (int max, int min)
    39. {
    40. return (rand ()%max) +min;
    41. }
    42.  
    43. int main()
    44. {
    45.  
    46. int tablica [3];
    47.  
    48. cout << "Podaj pierwsza liczbe " <<endl;
    49. cin >> tablica [0];
    50. cout << "Podaj druga liczbe" <<endl;
    51. cin >> tablica [1];
    52. cout << "Podaj trzecia liczbe" <<endl;
    53. cin >> tablica [2];
    54.  
    55. srand(time(0));
    56. int j=0;
    57.  
    58. do
    59. {
    60. int wylosowana = losowanie (3, 0);
    61. if (bezPowtorek (wylosowana, j) == false)
    62. {
    63. cout <<tablica[wylosowana];
    64. j++;
    65. }
    66. }while (j<2);
    67.  
    68. return 0;
    69. }
    70. [/cpp]
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Randomize value C++

    If you wish to draw two values out of three then the simplest way to do it is to draw one and throw it away taking all remaining values. You can also put all the values into a sorted array and then it's easy to determine if a new value is already in the table and throw it away.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.