Results 1 to 10 of 10

Thread: random

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default random

    Hello everybody,

    I have some numbers, for example 16, 20, 23, 40, 43

    I need to generate a random number of only this numbers i have.

    How could i make this on QT or C++ ??

    Can somebody help me?


    For the moment i have this:
    Qt Code:
    1. int max;
    2. QSqlQuery count("select count(words_id) from words_tbl");
    3. while(count.next())
    4. {
    5. max = count.value(0).toInt();
    6. }
    7.  
    8. //random
    9. int a = (1 + ( rand() % ( max - 1 + 1 ) ));
    10. QString b = QString::number(a);
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

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

    Default Re: random

    This is smart enough
    Qt Code:
    1. int fnc(){
    2. static int nmbrs[] = {16, 20, 23, 40, 43};
    3. int r = rand() % 5; // use something smarter than modulo here
    4. return nmbrs[r];
    5. }
    To copy to clipboard, switch view to plain text mode 

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

    raphaelf (23rd May 2007)

  4. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: random

    uau
    Thank you very much, you can think digital very well
    Think DigitalGasoline

  5. #4
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: random

    Hi everybody,

    With following sql statement i am able to rand a id from my database..
    Its works, but the number 1 come every 3. time i run it

    Have somebody a idea how to solve this by this sql statement?
    If it possible i would like to rand a id with sql..
    Qt Code:
    1. QString random;
    2. QSqlQuery count("SELECT words_id FROM words_tbl WHERE rowid>=random() % (SELECT max(words_id)+1 FROM words_tbl) LIMIT 1;");
    3.  
    4. while(count.next())
    5. {
    6. random = count.value(0).toString();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

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

    Default Re: random

    Do you initialise the random number generator for your sql server with a pseudorandom seed? Moreover, as I said, modulo is not the best operator here... How many rows to you have in that table?

    http://www.desilva.biz/mysql/random.html
    http://forums.devarticles.com/mysql-...l-rows-16.html
    http://www.tech-recipes.com/postgres...l_tips470.html
    http://www.powerpostgresql.com/Random_Aggregate

  7. #6
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Red face Re: random

    Hi wysota,
    I have on my sqlite database at the moment 250 rows, but the database will grow slowly..

    My sended script works good, but just the number 1 repeat a lot..
    The best solution for me would be that random numbers never repeats..

    Have you a idea how to rand a number, and until my app is runing is not possible to rand a same number?

    Thanks
    Think DigitalGasoline

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

    Default Re: random

    Quote Originally Posted by raphaelf View Post
    I have on my sqlite database at the moment 250 rows, but the database will grow slowly..

    My sended script works good, but just the number 1 repeat a lot..
    The best solution for me would be that random numbers never repeats..
    What if you run the lottery 251 times?

    Have you a idea how to rand a number, and until my app is runing is not possible to rand a same number?
    You can create a table with numbers 1-250 sequenced randomly and then when you want a random number, take a random row, remove it from the table and return the result. But again, what will happen if you want to have 251 random numbers?

  9. #8
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: random

    Hi wysota its a great idea..

    If i would run 251 i will get 0 probally?
    Think DigitalGasoline

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

    Default Re: random

    Quote Originally Posted by raphaelf View Post
    If i would run 251 i will get 0 probally?
    You won't get anything.

  11. #10
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: random

    Hi Wysota, thats ok for my app..

    Thank you, i will try it
    Think DigitalGasoline

Similar Threads

  1. opimize random
    By raphaelf in forum Newbie
    Replies: 2
    Last Post: 16th May 2007, 00:01
  2. Generating random characters
    By munna in forum General Discussion
    Replies: 1
    Last Post: 25th November 2006, 12:46
  3. Random no. generation using QT3??
    By darpan in forum General Programming
    Replies: 1
    Last Post: 8th August 2006, 12:02
  4. Random No Generator in C/C++
    By ankurjain in forum General Programming
    Replies: 1
    Last Post: 6th July 2006, 11:33
  5. Replies: 7
    Last Post: 31st May 2006, 09:37

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.