Page 1 of 2

Get a random between 2 numbers

PostPosted: Mon Jul 05, 2010 4:43 am
by Hblade
Heres how to get a random between 2 different numbers. The result is stored in a variable called "rv" so if you want to be able to use this random function correctly, use that variable.

First, put this in global code
Code: Select all
float rv;
void random(float first, float second)
{
    rv = rand(first+second);
    if (rv<first)
    {
        rv = first;
    }
    if (rv>second)
    {
        rv = second;
    }
}


Now when ever you wanna make a random between 2 numbers, simply put this:
Code: Select all
random(9, 32);

This will have rv store anything from 9 to 32, and everything in between. IF you want to use this without the decimals, than replace "float rv;" with "int rv"

Enjoy :D

Re: Get a random between 2 numbers

PostPosted: Mon Jul 05, 2010 4:48 am
by DST
float xx=rand(23)+9;

Re: Get a random between 2 numbers

PostPosted: Mon Jul 05, 2010 4:53 am
by Hblade
lol... still :P

Re: Get a random between 2 numbers

PostPosted: Mon Jul 05, 2010 5:01 am
by Bee-Ant
DST wrote:float xx=rand(23)+9;

In this case we want to use a custom function to use it...

This is the simpler version:
Code: Select all
int GetRandom(int first,int second)
{
    int arr[2],ran=rand(2);
    arr[0]=first;arr[1]=second;
    return arr[ran];
}


Usage:
Code: Select all
textNumber=GetRandom(9,23); //the output would be 9 or 23 randomly

Re: Get a random between 2 numbers

PostPosted: Mon Jul 05, 2010 5:41 am
by Hblade
n..nice :o

Re: Get a random between 2 numbers

PostPosted: Mon Jul 05, 2010 4:01 pm
by Hblade
However, this is getting random between 2 numbers, so your 9 - 23, in my script, it would put anything FROM 9 to 23, meaning anything in between, even with decimals :3 example it could be 14.27

Re: Get a random between 2 numbers

PostPosted: Mon Jul 05, 2010 4:20 pm
by Bee-Ant
Sorry, I didn't read well :oops:
Use this then...
Code: Select all
float GetRandomBetween(float ii,float jj)
{
    float ij=rand(max(ii,jj)-min(ii,jj));
    return ij+min(ii,jj);
}

Usage :
Code: Select all
textNumber=GetRandomBetween(9,23); //or
textNumber=GetRandomBetween(23,9);

Re: Get a random between 2 numbers

PostPosted: Mon Jul 05, 2010 4:22 pm
by Hblade
wow :D So short :) Good job

Re: Get a random between 2 numbers

PostPosted: Thu Jul 08, 2010 11:23 am
by NevenResnik
Can you please tell me how to make an actor create random actors in intervals (using a timer, or any other way.)

THANKS!!

Re: Get a random between 2 numbers

PostPosted: Thu Jul 08, 2010 1:59 pm
by Camper1995
Hi. I was not looking so much on the code, but if I understand that, you want to get random between 2 numbers. :D lol

I am making it this way:
Code: Select all
textNumber=ceil(rand(2));


That's all, if I understand what you need. :shock:

Re: Get a random between 2 numbers

PostPosted: Thu Jul 08, 2010 3:07 pm
by Bee-Ant
@Resnik:
Do you want to create different actor that have different names?

Put this code on Global code:
Code: Select all
#define Total 3
char Names[Total+1][16]={"Actor1","Actor2","Actor3",};

Put this code when creating actor, you can use timer or something:
Code: Select all
int index=rand(Total);
CreateActor(Names[index],"YourAnimationNameHere","(none)","(none)",0,0,false);

Note: in this case,adjust that Actor1,Actor2,Actor3 depend on your actor list name you want to create :D

@Camper: we want to get a random float number between 2 any numbers :D

Re: Get a random between 2 numbers

PostPosted: Fri Jul 09, 2010 9:07 am
by Camper1995
LOL :lol:

Ok, I know only 1 way to do it but I am sure you know it too because you are more advanced programmer
than me. With variable.

At start: var=ceil(rand(2));

then...
if var = 1 {textNumber = 4};
if var = 2 {textNumber = 28};

:oops:

Re: Get a random between 2 numbers

PostPosted: Fri Jul 09, 2010 10:44 am
by Hblade
lol camper ^.^ We already have one xD Not a bad method though. btw this wasnt a request lol, look at the first topic post xD

Re: Get a random between 2 numbers

PostPosted: Sat Jul 10, 2010 12:19 pm
by Camper1995
rofl. I was too lazy to scroll with mouse up. :D

Ok, sorry. Next time I will look what you need. :lol:

Re: Get a random between 2 numbers

PostPosted: Sat Jul 10, 2010 4:32 pm
by lcl
How about getting random between negative and positive number???
That will be very usable! :D