Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MrPoopypants

Random numbers!!! Argh...

This topic is 5730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys Quick question: How do i generate a random number by using the library. What i am trying to do is take four random numbers and assign them to variables. I have tried it many many ways and i keep getting errors.
    
unsigned  randomA = srand( time ( 0 ) ),
	  randomB = srand( time ( 0 ) ),
	  randomC = srand( time ( 0 ) ),
	  randomD = srand( time ( 0 ) );
    
If anyone could help me out here id really appreciate it Thanks! This is in C++ And some more things:
  
	srand ( time( 0 ) );
	
	do
	a = rand();
	b = rand();
	c = rand();
	d = rand();//Will doing ->  % 9 + 1 make it between 1 and 9?

	while( a != b && a != c && a != d && b != c && b != d && c != d );
  
i need to make these four digits not equal to one another and pass them to another function(or even main() for this matter)... doing
  
return a , b , c , d;
  
does not work thanks! [edited by - MrPoopypants on December 6, 2002 6:30:11 PM]

Share this post


Link to post
Share on other sites
Advertisement
well a few things.
first find a C/C++ reference or website that talks about generating random numbers.

now srand() is the seed for rand().
so when you put a number in srand() ex: srand (5), rand() will give you (pseudo)random numbers. ex: 6 4 2 9 1. but remember that it will always give you those numbers.

so you seed srand() with time ex: srand (unsigned time (NULL));
this way srand() is always getting a new number (unsigned makes them positive). then you can do something like to get a random number in a variable ex: int ran_num = rand();

i hope that helped.

remember Google is your friend!

Share this post


Link to post
Share on other sites
well, srand() is the seed function for the random number generator. you''d want to call srand, then call rand() for your variables.


  
srand(time(0));//assuming time() is a function and works

unsigned randomA = rand(),
randomB = rand(),
randomC = rand(),
randomD = rand();


My Homepage
How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.

Share this post


Link to post
Share on other sites
To make that a little more clear:

Call srand once. Usually it''s called with the time as the seed value.

Then call rand as many times as you like to get rand numbers. Do not call srand each time you call rand.

Share this post


Link to post
Share on other sites
Here's a bit of (untested) C code you can toy around with.


      
#include <stdio.h>
//

#include <stdlib.h>
//

#define ST_POWER 0
//

#define ST_DEFENSE 1
//

#define ST_AGILITY 2
//

void main(void)
{
int Stats[3];

srand(unsigned time (NULL));

//Note

//The % (modulo) is used to limit the value of rand, which is

//between 0 and 32767 (I think; might be -32767 and 32767.

//Maybe someone can validate this...?)

//

//So, we end up with a number between 0 and 19. The +1 makes it

//be between 1 and 20.

for(int i = 0; i <= 2; i++)
Stats[i] = rand()%20 + 1;

printf("Power: %d\n", Stats[ST_POWER ]);
printf("Defense: %d\n", Stats[ST_DEFENSE]);
printf("Agility: %d\n", Stats[ST_AGILITY]);
}
Yes, I use printf();. Bite me.

Like I said, untested, but it should work and if not, well, it should give you a good idea of how rand() and srand() operate. Try setting the parameter for srand() to a constant value like '42' or whatnot and run the program a few times to get the hang of it.

Oh yeah, one more thing... your code indicates only one use of "unsigned" and no actual variable declaration. You should do "unsigned int randomA = ..." etc for every variable otherwise you won't be able to compile without getting the compiler to barf out cryptic stuff like "undeclared identifier" and "do not hit me to make me compiler better".

Edit: the script tags are killing my source. Ignore the blank comment lines in it.

[edited by - RuneLancer on December 5, 2002 9:01:42 PM]

Share this post


Link to post
Share on other sites
Just a quick note: the VC++ CRT''s rand() sucks bigtime. Especially the lower bits are awful => don''t do stuff like rand()%x (alternatives: code up a better generator, e.g. Mersenne Twister, or shift down the higher bits and then mask.)

Share this post


Link to post
Share on other sites
Jan: I think it''s useful as far as being fast. This topic has been "explored" (beaten-to-death) here. Bonus is that I''m totally wrong for the first half of that post.

The MT is a better (randomness) RNG but not a better (speed) RNG. See here.

Nevertheless, you should never, ever use modulo with random numbers (unless your range is a power of two), and the reasons for that are explored in the first link I referenced above.

Julio: If you want to be a cool MS-basher with a witty tagline, you might wanna run a spell-check on it first.

HTTP 500 strike 1..2..

Share this post


Link to post
Share on other sites
Stoffel: sure MT is slower; there are also faster PRNGs than the VC++ (or should I say BASIC) rand().

Saw something in that other thread:
quote:
Stoffel
there''s no way you''d get a performance gain for copying the source for rand without inlining (which I highly suggest people do if they''re calling rand a lot, btw--just be careful in multithreaded environments).


quote:
DrPizza
I think that calling a fixed offset (which is what happens when I copy the code into my file) is faster than calling through a function pointer (which is what happens if you use a library version), so I think it may be possible even without inlining. However, it was inlining it anyway.


I had the same problem, and noticed the same thing. Differences WRT the static CRT are probably explained by inlining; the (MT) DLL version is *much* slower because 1) it has to get the thread''s random seed 2) indirect call through the IAT

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!