Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Clyde on October 07, 2009

Title: [C++] Random Numbers.
Post by: Clyde on October 07, 2009
Hi again,

How would you generate random numbers, in both integer and floats?

Im familiar with:

Quote from: Blitz
float value_flt=rnd(0.0f,0.360f);
int value_int=rand(64,128);


Cheers and all the very best,
Clyde.
Title: Re: [C++] Random Numbers.
Post by: Jim on October 07, 2009
In the C standard library you only have rand() which returns a number between 0 and RAND_MAX (which is usually, but not always, 32767).

So, if you want a whole number between 'from' and 'to' you need something like:
Code: [Select]
#include <stdlib.h>
...
int randrange(int from, int to)
{
return from + (rand()%(to-from));
}
If you want a floating point number between 0 and 1 then
Code: [Select]
#include <stdlib.h>
...
float rnd(void)
{
  return rand()/(float)RAND_MAX;
}

You should be able to build the other things you want from those.

Jim
Title: Re: [C++] Random Numbers.
Post by: Clyde on October 07, 2009
Cool and thanks :)

Im a bit stuck with the random float part.
Say I want a number between 2.45f and 5.75f.
Title: Re: [C++] Random Numbers.
Post by: Jim on October 07, 2009
Code: [Select]
float frand(float from, float to)
{
float clot=rand()/(float)RAND_MAX;
return from+clot*(to-from);
}

Jim
Title: Re: [C++] Random Numbers.
Post by: Clyde on October 07, 2009
Smart and thanks dude!

I've used srand((unsigned)time(0)); to randomly seed numbers based on the timer, like you do in Blitz And FreeBasic in the setup of my routine, not in the random functions. it's not generating different integers with the following:

Code: [Select]
int choice=random_int(0,1);

int random_int( int lowest, int highest )
{
int range=(highest-lowest);

return lowest + (rand()%(range));
}

btw with VC++ Express '08 you dont need to include the <stdlib.h>

Cheers again,
Clyde
Title: Re: [C++] Random Numbers.
Post by: Jim on October 07, 2009
You want random whole numbers between 0 and 1?  There is only one number in that range which is 0.  If you want the end number to be inclusive, then you need:
Code: [Select]
int range = to-from+1;
return from+(rand()%range);

Quote
btw with VC++ Express '08 you dont need to include the <stdlib.h>
Yes, you do.  It may look like it's working without it, but you must include it.

Jim
Title: Re: [C++] Random Numbers.
Post by: Clyde on October 18, 2009
Cheers Jim,

Just to clarify and peace of mind, would the random_float function be more or less the same for a random integers func ( signed and unsigned ) obviously with them not being floats?

Code: [Select]
int random_int( int lowest, int highest )
{
int clot=rand()/RAND_MAX;

int range = highest-(lowest+1);

return lowest+clot*(range);
             //return highest+(rand()%range);
}

Also what does 'clot' signify?

Thanks heaps,
Clyde.
Title: Re: [C++] Random Numbers.
Post by: Jim on October 18, 2009
If you want a float between A and B then
Code: [Select]
float random_float(float lowest, float highest)
{
  float r = (float)rand()/RAND_MAX;
  return lowest + r*(highest-lowest);
}

Jim
Title: Re: [C++] Random Numbers.
Post by: Shockwave on October 18, 2009
Quote
Also what does 'clot' signify?

Thanks heaps,


C ontingent L ocated in O peration T eritory
Title: Re: [C++] Random Numbers.
Post by: Clyde on October 24, 2009
^^Cheers! :)

Im a bit confused, I wonder could you post what would be the correct routines for generate ints and floats, in the range of lowest to highest, and plus so i can use methods like, that include the 0 ( actual lowest number )? I only ask as ive messed about abit experimenting and i dont know what the method was that worked ( as it doesnt know,hehe)

random_int( -2, -4 )
random_int( 0,5 )


Im really keen on making my own versions of routines that will make small exe's. Maybe a bit advanced stage, but I thought a random generator wouldnt be too taxing and without getting into Mersienne Twists; how could I code a simple random number generator without the need for big includes, or isnt the stdlib that big, and not time to reinvent the wheel with randomness?

I had thought on basing it on a sinus motion something like:

Code: [Select]
int randint( int min, int max, int div )
{
static int rand_inc;

rand_inc++;

int sum=max + sin(( (float) min * (float) rand_inc ) / div );

if ( sum < min ) sum=min;
if ( sum >=max ) sum=max;

return sum;
}

Or is there a better method?

(wow, that turned into another short story, i should get a publisher )

Cheers and many thanks,
Clyde.
Title: Re: [C++] Random Numbers.
Post by: Rbz on October 25, 2009
It's hard to understand what you are doing wrong but I believe that you don't know how to properly test those routines by Jim.

So, check out attached file below, it might help you out.
Title: Re: [C++] Random Numbers.
Post by: Clyde on October 25, 2009
Thanks RBZ, my random ints are working again!
Title: Re: [C++] Random Numbers.
Post by: Jim on October 25, 2009
If you want to generate your own random numbers, sin() isn't very useful because it repeats itself over and over.  Here's how many C libraries implement rand, because this code is suggested in the C standard.
Code: [Select]
static unsigned long int next = 1;
int rand(void)
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
srand() would just set 'next' to something else.

Jim
Title: Re: [C++] Random Numbers.
Post by: Clyde on November 15, 2009
I still am having problems in this area of Randomness.
my main one, is random_int(0,1) and it doesnt seem to want to play ball and returns one number only.
Title: Re: [C++] Random Numbers.
Post by: Jim on November 15, 2009
It's just a problem of definition.
If I ask you for a number between 0 and 1, what would you say?
000000
or
0101010100101001
?
In this case you need to ask for a whole number between 0 and 2.

Jim
Title: Re: [C++] Random Numbers.
Post by: Clyde on November 16, 2009
Ah ok, so I need to use

random_num=random_int(0,2);

I wonder if you can post some code for a solution for this, I need to generate a random integer based on a total amount, but I dont want it to give me a number that has already been used.

Huge thanks,
Clyde
Title: Re: [C++] Random Numbers.
Post by: Jim on November 16, 2009
So you want a routine that will give you all the numbers from 0 to 100 in a random order?
Code: [Select]
//initialise
int numbers[100];
for (int x=0; x<100; x++)
   numbers[x]=x;
int pick=100;
//numbers 0 to 99 in a random order
for (int y=0; y<100; y++)
{
  int n = random_int(0,pick);
  printf("%d\n", numbers[n]);
  pick--;
  numbers[n]=numbers[pick];
}

Jim
Title: Re: [C++] Random Numbers.
Post by: Clyde on November 16, 2009
yeah, and other ranges of numbers too, which I presume any 100's replace with desired amount? Also would the random_int routine be:

int random_int( int lowest, int highest )
{
return lowest+( (int) rand()%(highest-lowest));
}

I've ran it in console mode, and it gives more or less the same numbers, and not unique ones that havent been used before. Could you give me a lisiting of how you'd done things please dude?

I tried to redim the numbers[] array from within a void function by naming it static int numbers[amount];, it didnt like that.

Thanks again,
Clyde.
Title: Re: [C++] Random Numbers.
Post by: Jim on November 16, 2009
Yes, it's that random_int routine.

If you don't use srand() then rand() will always return the same numbers in the same order every time you restart your program.  A good way of calling it is
srand(time(NULL));

The new routine I just posted is a way of getting the numbers 0-99 in a random order without duplicates - more like dealing a deck of cards than a random number generator.  I was only guessing that that was what you wanted.  Are you saying it doesn't work?  I coded it in ps2yabasic then turned it in to C
Code: [Select]
dim numbers(100)
for x=0 to 99
   numbers(x)=x
next

pick=100
for y=0 to 99
  n = random_int(0,pick)
  print numbers(n),
  pick=pick-1
  numbers(n)=numbers(pick)
next

sub random_int(F,T)
return F+int(ran(T-F))
end sub
This works just fine.

It's not possible to resize an array in C.  You have to use malloc() and realloc() to do that.

Jim
Title: Re: [C++] Random Numbers.
Post by: Clyde on November 17, 2009
Big respect, cheers man.