Archived

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

question about generate random number in c++?

This topic is 5093 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

I am trying to generate 2 random numbers at the time is generate. However, it return only one number to main(). Does anyone know hat''s wrong with my code? below is my code of function start: ---------------------------------- int start(int max) { srand(time(0) % max); int number1; for (int i=0; i

Share this post


Link to post
Share on other sites
Only call srand() to seed the random number generator at the beginning of the program, not everytime you want a random number, and just seed it with time(0), then you should get some pseudorandom numbers.

Share this post


Link to post
Share on other sites
I could be wrong but I belive a function can only return one value thus the second value is ignored. Also you srand should be called at the start of the program and not each time.

As a side note does anyone know if you can use a fixed seed at some parts of your code and random seed at other parts?

-----------------------------------------------------
Writer, Programer, Cook, I''m a Jack of all Trades
Current Design project
Chaos Factor Design Document

Share this post


Link to post
Share on other sites
as TechnoGoth said, you can only return one variable from a function. the only reason that you didn''t get an error is because the comma (,) operator is valid in C++ (it evaluates both parts before and after it, but only returns the last one).
quote:
Original post by TechnoGoth
As a side note does anyone know if you can use a fixed seed at some parts of your code and random seed at other parts?

i don''t think so... not with the default psuedo-random number generator anyway.

there is a class implementation of the Mersenne Twister, so you could make multiple object instances off the class and seed each one differently... you could try that. sorry but i have no link

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
void Return2RandomNums(int *num1, int *num2) {
*num1 = WhateverYourRandFuncIs();
*num2 = WhateverYourRandFuncIs();

return;
}


call this with:

int x,y;
Return2RandomNums(&x, &y);

Share this post


Link to post
Share on other sites
Or better (since this is c++):
void Return2RandomNums(int& num1, int& num2) {
num1 = WhateverYourRandFuncIs();
num2 = WhateverYourRandFuncIs();
return;
}


Enigma

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Link to <a href="http://www.math.keio.ac.jp/~matumoto/emt.html">Mersene Twister</a> page

quote:
there is a class implementation of the Mersenne Twister, so you could make multiple object instances off the class and seed each one differently


Why would anyone want to be-able to do that if they want entirely random numbers ? surely you have a danger of predictable numbers that way...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Damn it! try with capitals in my tags like others (...or can''t AP''s post links ?)

Link to <A HREF="http://www.math.keio.ac.jp/~matumoto/emt.html">Mersene Twister</A> page

Share this post


Link to post
Share on other sites
quote:
Original post by Tenchi
return number1, number2;



You cant return two values at once. The compiler treats the comma as the sequential evaluation operator. That line of code is essentially equivalent to return number2;

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Why would anyone want to be-able to do that if they want entirely random numbers ? surely you have a danger of predictable numbers that way...

it would be good for, say, randomly generated maps (that you don''t want to change mid-game)... instead of saving the entire map for the next time, you just save the seed and generate it again.

anonymous posters can''t use HTML, btw.

Share this post


Link to post
Share on other sites
If you want to do real C++ (without inventing something new) try this:


#include <pair> // or <algorithm>


std::Pair< int, int > random_numbers()
{
static bool init = false;
if( !init ) {
init = true;
srand( time( 0 ) );
}
return std::Pair< int, int >( rand(), rand() );
}

int
main()
{
std::Pair< int, int > theNumbers = random_numbers();
printf( "%d %d\n", theNumbers.first, theNumbers.second );
return 0;
}




[edited by - hplus0603 on February 7, 2004 11:21:21 PM]

Share this post


Link to post
Share on other sites