Skip to main content
gamedev.net
🔒 Locked

Random number

Started by Program_mike Jun 19, 2007 at 7:37 PM 16 replies 2k views
Original Post
Program_mike
Program_mike
i need a code to chose a even random number between 10 and 36 using C++ , and i am very new at this [Edited by - Program_mike on June 19, 2007 8:21:47 PM]
Pzc
Pzc
Psuedocode that should give you the general idea:

range = max - mintemp_number = rand(range)      or c-ish      number = rand() % rangenumber = min + temp_number


Need to know how the random-function works though, if it includes zero and the like. Including what language you use would've helped ;)
yadango
yadango
13 ;)
Chaotic_Attractor
Chaotic_Attractor
In C++:
int num = 1;while ((num%2) != 0)   num = rand()%27 + 10;


This is assuming that 10 and 36 are included.
Strength without justice is no vice; justice without strength is no virtue.
Xentropy
Xentropy
Quote:
Original post by Chaotic_Attractor
In C++:
*** Source Snippet Removed ***

This is assuming that 10 and 36 are included.


The problem with that method is the possibility (though miniscule) of becoming an infinite loop if odd numbers kept coming up. As a rule, I try to avoid loops that last a random amount of time before coming up with the result I want.

A better method would be to get a number between 5 and 18 (just once every time) and then double it.
Chaotic_Attractor
Chaotic_Attractor
Quote:

The problem with that method is the possibility (though miniscule) of becoming an infinite loop if odd numbers kept coming up.


I suppose so, but the chance of that actually affecting the performance of the program is so low that it is practically zero. And technically, you could not have an infinite loop here since at infinity 53.8% of the numbers chosen would be even.
Strength without justice is no vice; justice without strength is no virtue.
owl
owl
something among the pseudo-line of:

int Random_Even(){  int number = Random_Number(10,36);  if (is_Odd(number))      number++;  return number;}
[size="2"]I like the Walrus best.
Xentropy
Xentropy
True, it couldn't become an infinite loop, but it's still an indefinite loop. Why not avoid the indefinite method if there's something as easy as rnd(5-18) * 2 available?

Quote:
Original post by owl
something among the pseudo-line of:

int Random_Even(){  int number = Random_Number(10,36);  if (is_Odd(number))      number++;  return number;}


Sorry to be nitpicky, but that's even worse. There's exactly half the probability of 10 at the output than any one of the values 12-36. Using 9-36 as the range would work though.
Zahlman
Zahlman
Quote:
Original post by Program_mike
[choose an] even random number between 10 and 36 using C++


What for?
Program_mike
Program_mike
Quote:
Original post by owl
something among the pseudo-line of:

int Random_Even(){  int number = Random_Number(10,36);  if (is_Odd(number))      number++;  return number;}


it says "
implicit declaration of function `int Random_Number(...)'
rip-off
rip-off
Quote:
Original post by Program_mike
Quote:
Original post by owl
something among the pseudo-line of:

int Random_Even(){  int number = Random_Number(10,36);  if (is_Odd(number))      number++;  return number;}


it says "
implicit declaration of function `int Random_Number(...)'


Hence the "pseudo-line", a reference to "psuedo-code", which means the code should look something like this. You would need to write "Random_Number" and "is_Odd" yourself for the above to compile.

Pzc gave you code which could be used as the body of a Random_Number() function, and Chaotic_Attractor has a snippet which contains (among other things) an implementation of the body of is_Odd.

Try yourself and see how far you get.
Bregma
Bregma
Quote:
Original post by yadango
13 ;)
Ah, I think you mean this.
  int val = (rand() % 13 + 5) * 2;
Stephen M. Webb
Professional Free Software Developer
yadango
yadango
Quote:
Original post by Bregma
Quote:
Original post by yadango
13 ;)
Ah, I think you mean this.
  int val = (rand() % 13 + 5) * 2;


does that work?
% 13 returns 0 to 12...
+ 5 so 5 to 17...
times 2 so 10 to 34...
so should be % 14 :-).

but no he he he, that's not what i meant lol. i was just having some fun. his original post (before he edited it) was "i need a random number between 10 and 36." :-)
empirical2
empirical2
int Val=((rand() % 26) + 10) & 126;
Program_mike
Program_mike
Quote:
Original post by rip-off
Quote:
Original post by Program_mike
Quote:
Original post by owl
something among the pseudo-line of:

int Random_Even(){  int number = Random_Number(10,36);  if (is_Odd(number))      number++;  return number;}


it says "
implicit declaration of function `int Random_Number(...)'


Hence the "pseudo-line", a reference to "psuedo-code", which means the code should look something like this. You would need to write "Random_Number" and "is_Odd" yourself for the above to compile.

Pzc gave you code which could be used as the body of a Random_Number() function, and Chaotic_Attractor has a snippet which contains (among other things) an implementation of the body of is_Odd.

Try yourself and see how far you get.


could you help me with those?

Pzc
Pzc
Untried, from the top-of-my-head code which may or may not work as intended:

int Random_Even( int min, int max ){  int temp = min + ( rand() % ( max - min + 1 ) );  if( temp % 2 == 1 )    ++temp;  return temp;}


You probably want to add some checks to min and max but that's up to you.
instinKt
instinKt
Quote:
Original post by Zahlman
Quote:
Original post by Program_mike
[choose an] even random number between 10 and 36 using C++


What for?


I smell homework.
Program_mike
Program_mike
Quote:
Original post by instinKt
Quote:
Original post by Zahlman
Quote:
Original post by Program_mike
[choose an] even random number between 10 and 36 using C++


What for?


I smell homework.


no just making a simple mind/math game

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.