Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random function C++
02-19-2010, 11:33 PM (This post was last modified: 02-19-2010 11:36 PM by drdebcol.)
Post: #1
Random function C++
I was trying to make a string randomizer function for this task :
http://www.pro9ramming.com/number-patter...-1123.html
because i needed to randomize string that is long in order to test speed of algorithm in cases that are described in that thread.
I had problems with that. First i had to make a string with characters that are digits, so i just thought to randomize numbers that are in interval 48 to 58 (those are actually ASCII characters for digits 0..9), but because i called rand() function in the same time (i mean mili-second that is 1/1000 seconds) it was generating string with same digits, for example "66666666", but i needed random string as for example "47634221". I was searching on the internet, but noting, they have only functions for generating a single number.
So i decided to make my own random function.
Basically if you want to format rand() function to generate numbers in a certain interval, you do it like this :
Code:
rand()%100+1 //Numbers will be generated in interval 1..100
And you need to use srand() function that initializes random number generator. The pseudo-random number generator is initialized using the argument passed as seed.

For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.
Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header <ctime>) is different each second, which is distinctive enough for most randomizng needs.
And inside of that srand() function i called time() function that returns how many seconds passed from 1.1.1970 till now and i added rand() function on to time() function and that makes srand() really non-predictable and because of that rand() returns random numbers whenever it is called, even in the same mili-second.
So here is that random function :
Code:
int random(int lowest, int highest)
{
srand(time(0)+rand());
int random_integer;
int range=(highest-lowest)+1;
for(int i=0; i<20; i++)
{
  random_integer=lowest+int(range*rand()/(RAND_MAX+1.0));
}
return random_integer;
}
You need to have <iostream> included for it.
XIO-X2 suggested to make a sleep function and to call it inside of random function for 3 mili-seconds "sleep(3)". I made that and tested, but it is really slow because of that period of delay.
I used this sleep() function for that :
http://www.pro9ramming.com/sleep-functio...t-965.html
Now proof for everything. You can compile this code :
Code:
# include <iostream>

using namespace std;

int random(int lowest, int highest)
{
srand(time(0)+rand());
int random_integer;
int range=(highest-lowest)+1;
for(int i=0; i<20; i++)
{
  random_integer=lowest+int(range*rand()/(RAND_MAX+1.0));
}
return random_integer;
}


int main()
{
  cout<<rand()%100+1<<endl;
  cout<<rand()%100+1<<endl;
  cout<<random(1,100)<<endl;
  cout<<random(1,100)<<endl;
  system("pause");  
}
And you'll see that the first two numbers that are outputted with rand() function are always the same, And second two that are outputted with my random() function are always different on every compiling.
Also you can use this code :
Code:
# include <iostream>

using namespace std;

int random(int lowest, int highest)
{
srand(time(0)+rand());
int random_integer;
int range=(highest-lowest)+1;
for(int i=0; i<20; i++)
{
  random_integer=lowest+int(range*rand()/(RAND_MAX+1.0));
}
return random_integer;
}


int main()
{
  for (int i=0;i<10;i++)
  cout<<random(1,100)<<endl;
  system("pause");  
}

It will output 10 random numbers and every time you run the program the will be different.
I started this program 3 times and this is what i got :
[Image: 40988_random1.jpg]
[Image: 40989_random2.jpg]
[Image: 40990_random3.jpg]

I hope that you like this function !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
02-20-2010, 05:07 PM
Post: #2
RE: Random function C++
Better than the rand() function! thanks for this

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
02-22-2010, 02:09 AM
Post: #3
RE: Random function C++
Nice function...
Just a note: There is a Randomize() fucntion in Pascal and C# which is used to rerandomize seeds used by the Random() function... Perhaps there is a similar function in C or C++... I'm not sure, but it should be in some of the librarys...
The calling procedure is then simpler:
Code:
Randomize();
int x = Random(a,b);

MoneyBookers.com - Internet Banking

When the power of love overcomes the love of power, the world will know peace.
Find all posts by this user
Quote this message in a reply
02-22-2010, 07:19 AM
Post: #4
RE: Random function C++
(02-22-2010 02:09 AM)XIO-X2 Wrote:  Nice function...
Just a note: There is a Randomize() fucntion in Pascal and C# which is used to rerandomize seeds used by the Random() function... Perhaps there is a similar function in C or C++... I'm not sure, but it should be in some of the librarys...
The calling procedure is then simpler:
Code:
Randomize();
int x = Random(a,b);
Yes i explained that above it is srand() function. But it is not enough random even after implementing it. So you can use rand() inside of srand() and than you can get real random. Everything is explained above.

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
02-23-2010, 01:39 AM
Post: #5
RE: Random function C++
Oh... Okay... Sorry... Smile Just wanted to point that out... Smile I only took a short look of the code... Usually I don't read explanations... Smile Big Grin The code is clear enough for me... Smile
But the definition of a "random" number requires these numbers to be shown in a relatively even number of representations of each one over longer perods of time... Smile The mathematical requirement may be a problem for this, but all in all it's a good random function... Smile

MoneyBookers.com - Internet Banking

When the power of love overcomes the love of power, the world will know peace.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: