Author Topic: Random number question  (Read 2636 times)

Djjammin

  • Guest
Random number question
« on: August 15, 2012, 01:44:06 pm »
might be a really simple answer to this but how do you get a random number between to ints IE between 500-2000

is there an easier way than creating a loop and trying out random numbers until you get one over 500?

Many Thanks in Advance

Jammin

xabaras

  • Guest
Re: Random number question
« Reply #1 on: August 15, 2012, 03:16:54 pm »
i often have a similar need... i created my simple function...

Code: [Select]
int MyRandom(int min, int max) {
    int random = -1;
while ( random < min ) {
        random = Random(max);
}
return random;
}

Then, in AppMain() put something like RandomSetSeed(time(0)); (in order to have real random numbers) ... and when you need your random number, just call something like ran = MyRandom(500,2000);

Djjammin

  • Guest
Re: Random number question
« Reply #2 on: August 16, 2012, 10:23:30 am »
That's a good idea, thanks mate! I have a history of coding in VB so have picked up some terrible habits and laziness!

jousepo

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +0/-0
    • View Profile
Re: Random number question
« Reply #3 on: August 23, 2012, 09:14:07 am »
More simple:

Code: [Select]
int QuickRandom(int min, int max) {
    return Random(max+min)-min;
}

ljowahir

  • Guest
Re: Random number question
« Reply #4 on: September 08, 2012, 07:39:12 pm »
Just add 500 to a random number between 0 and 1499

Djjammin

  • Guest
Re: Random number question
« Reply #5 on: September 13, 2012, 01:04:53 pm »
More simple:

Code: [Select]
int QuickRandom(int min, int max) {
    return Random(max+min)-min;
}

Tried to use this but it returns Minus numbers, anyway to remove minus numbers?

Djjammin

  • Guest
Re: Random number question
« Reply #6 on: September 13, 2012, 01:11:41 pm »
i often have a similar need... i created my simple function...

Code: [Select]
int MyRandom(int min, int max) {
    int random = -1;
while ( random < min ) {
        random = Random(max);
}
return random;
}


Also tried this and if i try and do it for numbers between 20000,50000 it crashes the app... any advise would be appreciated!

Thanks
Then, in AppMain() put something like RandomSetSeed(time(0)); (in order to have real random numbers) ... and when you need your random number, just call something like ran = MyRandom(500,2000);

stuckerj

  • Guest
Re: Random number question
« Reply #7 on: September 13, 2012, 08:59:44 pm »
Got the signs reversed above.  Try this:

Code: [Select]
int QuickRandom(int min, int max) {
    return Random(max-min)+min;
}

jousepo

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +0/-0
    • View Profile
Re: Random number question
« Reply #8 on: September 14, 2012, 05:44:07 am »
i'm wrong!!!! stuckerj is right....