|
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..100For 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)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>Also you can use this code : Code: # include <iostream>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 : ![]() ![]() ![]() 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 |
|||
|
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 |
|||
|
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();MoneyBookers.com - Internet Banking When the power of love overcomes the love of power, the world will know peace. |
|||
|
02-22-2010, 07:19 AM
Post: #4
|
|||
|
|||
RE: Random function C++
(02-22-2010 02:09 AM)XIO-X2 Wrote: Nice function...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 |
|||
|
02-23-2010, 01:39 AM
Post: #5
|
|||
|
|||
|
RE: Random function C++
Oh... Okay... Sorry...
Just wanted to point that out... I only took a short look of the code... Usually I don't read explanations... The code is clear enough for me... ![]() 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... The mathematical requirement may be a problem for this, but all in all it's a good random function...
MoneyBookers.com - Internet Banking When the power of love overcomes the love of power, the world will know peace. |
|||
|
« Next Oldest | Next Newest »
|



![[Image: 40988_random1.jpg]](http://myph.us/pics/40988_random1.jpg)
![[Image: 40989_random2.jpg]](http://myph.us/pics/40989_random2.jpg)
![[Image: 40990_random3.jpg]](http://myph.us/pics/40990_random3.jpg)





Just wanted to point that out...
The code is clear enough for me...