Advertisement

Random

Started by October 19, 2000 01:33 PM
16 comments, last by SKSlayer 24 years, 1 month ago
How can I get a random number ? I mean really random cuz rand()%65535 always output the same number the first time ... And as I only need the first output, it's always the same :o( Or, how do I get system hours/minutes/seconds ? into one/tree vars ??? And how about system date ? This is TOTALLY off topic, but I need it for a GL prog, so, I first thought about posting it here Edited by - SKSlayer on 10/19/00 2:36:17 PM
(you can find me on IRC : #opengl on undernet)
Hmm, I seem to remember a randomize() function that might mix up the numbers for you. I am not too sure if that is C++ though...

take a look. If randomize is right, then just call it at the beginning of your prog and you should have totally random numbers

L8r,
The Rainmaker
Biendschmofan''s Tutorials Of OpenGL Glander
L8r,[email=richardfrazee@msn.com]The Rainmaker[/email]Biendschmofan''s Tutorials Of OpenGL Glander
Advertisement
Try seeding your rand() function with time or anouther changing variable. Time usially works preaty good.

Roach
quote: Original post by Roach

Try seeding your rand() function with time or anouther changing variable. Time usially works preaty good.

Roach


By the way, there is no such thing as a totially random number in C or c++ (or any programming language for that matter).

roach is correct about there being no ''real'' random number creation. However it should be ''random'' enough for your/usually anybody ''s purposes.

On a side note, i would go as far as saying that there is no random, period. Everything happens because of something. Another comment, i also think that (almost, if not) everything is mathmatically based...

my 2 cents

-mike
I had many times this problem, and solved it with the following command :

srand( (unsigned)time( NULL ) );

You need to call that command only one time in your program and rand should give you "real" random number.

My Web Page
Advertisement
Well, well, well... So you think there''s no such thing, eh? Well, you''re almost right... You can get pretty close to completely random by seeding the random number... All you have to do is...

1. Include time.h in your program (or the .cpp file you are using for the random number thing...) also stdlib.h, but I''m sure you''ve already got that if you''re using random numbers...

2. find a spot that you can conveniently enter a line of code (you might want to find this spot before the random number is generated. Also, it''s a good idea to keep it just in one spot, not in a loop since you don''t need to tell it where to seed the random numbers from more than once...)

3. seed the random numbers with the current time (from 1980, i think... in seconds... maybe... doesn''t matter, it''s different almost everytime anyway. )

The line of code you have to use is....

Drumroll please... (man, am I building suspense, or what?) I know, I could''ve said the same thing in about two lines, but this is more fun and it makes me seem more knowledgable...

srand((unsigned int)time(NULL))

There ya go.

S.
Bullshit .. Try this and see the result.
Create a DLL that returns a random number from one of the functions A C/C++ DLL then access that DLL using another. In that function seed it with as you have it with time and then return that number. call it multiple times in a row and you will get the same number. your system returns the time in a specific way such that the times are almost the same in ''seconds'' and it rounds it so you will get the same number. try seeding with the ''milliseconds'' instead for a more accurate number. Least this is my experience with creating a DLL for WinRunner and using seed and rand(). Though if seed it then call it multiple times, the rand function the you will random numnbers.

Or something along those lines... I''m pretty doped on vicotin cause just got wisdom teeth removed.


Whoa!! You can think c++ and type at the same time as being sedated?! I was out for an entire day after my wisdom teeth! I don''t remember anything!

S.
I found out that playing with actual time can get pretty look like random numbers
(you can find me on IRC : #opengl on undernet)

This topic is closed to new replies.

Advertisement