Results 1 to 8 of 8

Thread: How to generate an array of random numbers

  1. #1
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default How to generate an array of random numbers

    Hi,


    I want to generate an array of random alphabets from A to H. As of my knowldge I can use rand() function to generate but seems bit complex and require more lines of code. I would like to know If there is any Qt function like in Java that can quickly generate numbers.

    Thank You,

    Baluk

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

    Default Re: How to generate an array of random numbers

    What's more quickly than calling rand()?
    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.


  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to generate an array of random numbers

    You can use qrand

  4. #4
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to generate an array of random numbers

    Hi,

    I have tried using the qrand() but I can't able to get the desired output. I want to generate a random set with non-repeatable alphabets. I have tried like this
    Qt Code:
    1. int array[] = {65,66,67,68,69,70,71,72};
    2. qsrand(time(0));
    3. for(i=0;i < SequenceCount; i++)
    4. {
    5. int j = (qrand() % SequenceCount );
    6. qDebug() << array[j];
    7. }
    To copy to clipboard, switch view to plain text mode 

    But in the output I see some repeatable numbers.

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

    Default Re: How to generate an array of random numbers

    You mean you want to have a random permutation of the array?

    Qt Code:
    1. #include <algorithm>
    2.  
    3. int array[] = {65,66,67,68,69,70,71,72};
    4. std::shuffle(array, array+8);
    To copy to clipboard, switch view to plain text mode 
    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.


  6. #6
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to generate an array of random numbers

    Yes I need the same functionality U have given me. But the statement
    std::shuffle(array, array+8);
    is not recognized. it says "shuffle" is not a member of the std.

    then I used
    Qt Code:
    1. std::rand_shuffle(array, array);
    To copy to clipboard, switch view to plain text mode 
    but it is giving me some extra garbage values along with the result. like
    EBGCAFHD Ã…
    @|%
    .

  7. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to generate an array of random numbers

    The function name is random_shuffle and you need to include <algorithm>

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to generate an array of random numbers

    If you print something to the console, it has to end with \0. This is basic C/C++ knowledge.
    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.


  9. The following user says thank you to wysota for this useful post:

    baluk (22nd November 2010)

Similar Threads

  1. Replies: 2
    Last Post: 12th November 2010, 14:42
  2. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24
  3. Replies: 1
    Last Post: 7th April 2010, 16:26
  4. Qt large random numbers
    By timmu in forum Qt Programming
    Replies: 11
    Last Post: 31st August 2009, 08:22
  5. random
    By raphaelf in forum General Programming
    Replies: 9
    Last Post: 6th June 2007, 12:33

Tags for this Thread

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.