Author Topic: [C++] Random Numbers.  (Read 10488 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
[C++] Random Numbers.
« 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.
« Last Edit: October 07, 2009 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Random Numbers.
« Reply #1 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
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Random Numbers.
« Reply #2 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.
« Last Edit: October 07, 2009 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Random Numbers.
« Reply #3 on: October 07, 2009 »
Code: [Select]
float frand(float from, float to)
{
float clot=rand()/(float)RAND_MAX;
return from+clot*(to-from);
}

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Random Numbers.
« Reply #4 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
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Random Numbers.
« Reply #5 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
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Random Numbers.
« Reply #6 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.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Random Numbers.
« Reply #7 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
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: [C++] Random Numbers.
« Reply #8 on: October 18, 2009 »
Quote
Also what does 'clot' signify?

Thanks heaps,


C ontingent L ocated in O peration T eritory
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Random Numbers.
« Reply #9 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.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: [C++] Random Numbers.
« Reply #10 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.
« Last Edit: October 25, 2009 by rbz »
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Random Numbers.
« Reply #11 on: October 25, 2009 »
Thanks RBZ, my random ints are working again!
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Random Numbers.
« Reply #12 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
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Random Numbers.
« Reply #13 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.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Random Numbers.
« Reply #14 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
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Random Numbers.
« Reply #15 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
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Random Numbers.
« Reply #16 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
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Random Numbers.
« Reply #17 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.
« Last Edit: November 16, 2009 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Random Numbers.
« Reply #18 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
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Random Numbers.
« Reply #19 on: November 17, 2009 »
Big respect, cheers man.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: