PDA

View Full Version : random



raphaelf
21st May 2007, 08:04
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:


int max;
QSqlQuery count("select count(words_id) from words_tbl");
while(count.next())
{
max = count.value(0).toInt();
}

//random
int a = (1 + ( rand() % ( max - 1 + 1 ) ));
QString b = QString::number(a);

wysota
21st May 2007, 08:47
This is smart enough :)

int fnc(){
static int nmbrs[] = {16, 20, 23, 40, 43};
int r = rand() % 5; // use something smarter than modulo here
return nmbrs[r];
}

raphaelf
23rd May 2007, 09:08
uau :D
Thank you very much, you can think digital very well :p

raphaelf
1st June 2007, 08:05
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? :crying:
If it possible i would like to rand a id with sql..


QString random;
QSqlQuery count("SELECT words_id FROM words_tbl WHERE rowid>=random() % (SELECT max(words_id)+1 FROM words_tbl) LIMIT 1;");

while(count.next())
{
random = count.value(0).toString();
}

wysota
1st June 2007, 09:41
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-development-50/getting-random-mysql-rows-16.html
http://www.tech-recipes.com/postgresql_pgsql_tips470.html
http://www.powerpostgresql.com/Random_Aggregate

raphaelf
5th June 2007, 18:27
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

wysota
5th June 2007, 20:31
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?

raphaelf
6th June 2007, 08:00
Hi wysota its a great idea..

If i would run 251 i will get 0 probally?

wysota
6th June 2007, 11:41
If i would run 251 i will get 0 probally?

You won't get anything.

raphaelf
6th June 2007, 13:33
Hi Wysota, thats ok for my app..

Thank you, i will try it :)