Get a random between 2 numbers

Talk about making games.

Get a random between 2 numbers

Postby Hblade » Mon Jul 05, 2010 4:43 am

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
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Get a random between 2 numbers

Postby DST » Mon Jul 05, 2010 4:48 am

float xx=rand(23)+9;
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Get a random between 2 numbers

Postby Hblade » Mon Jul 05, 2010 4:53 am

lol... still :P
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Get a random between 2 numbers

Postby Bee-Ant » Mon Jul 05, 2010 5:01 am

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
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: Get a random between 2 numbers

Postby Hblade » Mon Jul 05, 2010 5:41 am

n..nice :o
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Get a random between 2 numbers

Postby Hblade » Mon Jul 05, 2010 4:01 pm

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
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Get a random between 2 numbers

Postby Bee-Ant » Mon Jul 05, 2010 4:20 pm

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);
Last edited by Bee-Ant on Mon Jul 05, 2010 4:34 pm, edited 2 times in total.
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: Get a random between 2 numbers

Postby Hblade » Mon Jul 05, 2010 4:22 pm

wow :D So short :) Good job
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Get a random between 2 numbers

Postby NevenResnik » Thu Jul 08, 2010 11:23 am

Can you please tell me how to make an actor create random actors in intervals (using a timer, or any other way.)

THANKS!!
NevenResnik
 
Posts: 42
Joined: Mon May 31, 2010 9:59 am
Score: 0 Give a positive score

Re: Get a random between 2 numbers

Postby Camper1995 » Thu Jul 08, 2010 1:59 pm

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:
Say hello to my little friend.
User avatar
Camper1995
 
Posts: 707
Joined: Tue Dec 30, 2008 7:20 pm
Location: Lost in the past.
Score: 44 Give a positive score

Re: Get a random between 2 numbers

Postby Bee-Ant » Thu Jul 08, 2010 3:07 pm

@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
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: Get a random between 2 numbers

Postby Camper1995 » Fri Jul 09, 2010 9:07 am

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:
Say hello to my little friend.
User avatar
Camper1995
 
Posts: 707
Joined: Tue Dec 30, 2008 7:20 pm
Location: Lost in the past.
Score: 44 Give a positive score

Re: Get a random between 2 numbers

Postby Hblade » Fri Jul 09, 2010 10:44 am

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
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Get a random between 2 numbers

Postby Camper1995 » Sat Jul 10, 2010 12:19 pm

rofl. I was too lazy to scroll with mouse up. :D

Ok, sorry. Next time I will look what you need. :lol:
Say hello to my little friend.
User avatar
Camper1995
 
Posts: 707
Joined: Tue Dec 30, 2008 7:20 pm
Location: Lost in the past.
Score: 44 Give a positive score

Re: Get a random between 2 numbers

Postby lcl » Sat Jul 10, 2010 4:32 pm

How about getting random between negative and positive number???
That will be very usable! :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Next

Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest