Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Random Number Generator

Display your little helpful snippets of code that may help us all get something done

Re: Random Number Generator

Postby antiRTFM on Mon Jun 15, 2009 10:26 pm

Awesome post yellowaeroplane! Thanks for your input.

As you see from the thread, the GetRandom function went through quite some changes to accomodate for different situations (like generating zeros etc)

So now you've come up with another issue... well found! That is some extensive testing you've done with it there. And the suggested solution looks good too (that huge return statement was beginning to look real ugly...)

One thing though i remember reading that using the % operator with rand() doesnt yield best results because of the way the % operator shifts bits. As in contrast to the / operator which does yield best "randomified" bits.

So; can we come up with a solution that uses the / operator, and will work fine when i want to generate random binary (1s and 0s) and will pass your lottery test case...
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Got It!

Postby yellowaeroplane on Mon Jun 15, 2009 11:33 pm

You are a hard man to please antiRTFM.

You are correct in the assumption that the modulus operator actually favors the smaller numbers in your range, but it only becomes apparent when working with large ranges.

So here's something better...

I've been busting my brain over this one, but here we go. It's (as far as I know) impossible to lose the gigantic return statement, but from what my tests show, the following fulfills your other requirements. You had it so close in the beginning that it's actually funny:

Johnny's final *perfect* random number generator:

Code: Select all
int GetRand(int rangeMin, int rangeMax) {
    return (int)floor(rand()/(RAND_MAX+1.0)*((rangeMax-rangeMin)+1)+rangeMin);
}


I'm going to go enjoy a nice cold adult beverage now.

:wink:

Happy coding!
yellowaeroplane
 
Posts: 3
Joined: Mon Jun 15, 2009 8:58 pm

Re: Random Number Generator

Postby antiRTFM on Tue Jun 16, 2009 1:18 am

Alright here we go. I'm editing the original post once more...

BTW does it pass your lottery test? can you show us some numbers using the above code in your lottery test?
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Random Number Generator

Postby yellowaeroplane on Tue Jun 16, 2009 1:26 pm

It sure does.

Here are the results for three runs:

Run 1
John won 979 times.
Brad won 959 times.
Jose won 975 times.
Nick won 998 times.
Tom won 959 times.
Chris won 1017 times.
Zach won 1012 times.
Matt won 1046 times.
George won 1012 times.
Steve won 1043 times.

Run 2
John won 1043 times.
Brad won 1068 times.
Jose won 941 times.
Nick won 971 times.
Tom won 993 times.
Chris won 995 times.
Zach won 989 times.
Matt won 1021 times.
George won 964 times.
Steve won 1015 times.

Run 3
John won 985 times.
Brad won 1003 times.
Jose won 993 times.
Nick won 1051 times.
Tom won 974 times.
Chris won 992 times.
Zach won 961 times.
Matt won 1027 times.
George won 1013 times.
Steve won 1001 times.

It seems like it works pretty well to me, but let me know what you think.

:)
yellowaeroplane
 
Posts: 3
Joined: Mon Jun 15, 2009 8:58 pm

Re: Random Number Generator

Postby antiRTFM on Tue Jun 16, 2009 5:53 pm

Looks good!
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Random Number Generator

Postby herd on Fri Jul 03, 2009 11:06 am

Somehow I dont get any number at all!
Code: Select all
#include "stdafx.h"
#include <cstdlib>      // For srand() and rand()
#include <ctime>      // For time()
#include <iostream>
using namespace std;

srand((long)time(0));

int GetRand(int num1, int num2)
{
    int & rangeMin = num1 > num2 ? num2 : num1;
    int & rangeMax = num1 > num2 ? num1 : num2;
    return (int)floor(rand() / (RAND_MAX + 1.0) * (rangeMax - rangeMin) + rangeMin + 0.5);
}

int main()
{
   cout << GetRand(1,10);
   cout << endl;
   system("Pause");
    return 0;
}
herd
 
Posts: 15
Joined: Wed Jul 01, 2009 12:42 pm

Re: Random Number Generator

Postby antiRTFM on Fri Jul 03, 2009 11:45 am

@herd: I've edited that post where you got this code from. However you've also added some code of your own up there. Follow the instructions of the first post in this thread and make sure you-

1. don't include <cstdlib>
2. include <cmath>
3. put srand((long)time(0)) inside the main function
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Random Number Generator

Postby herd on Fri Jul 03, 2009 11:59 am

ok thx, working now.

But as I know rand() function gives only pseudo random numbers with NO equal spacing for the numbers.
As far as I understand that means that the numbers DONT have same exact chance to be shown up.

Is that true? Is there another better way to generate equally spaced random numers?

I mean if you want to have random number from 1 to 10 you do want that each of these numbers has the chance of 10%. It would be stupid if the probability would vary.

Thx.
herd
 
Posts: 15
Joined: Wed Jul 01, 2009 12:42 pm

Re: Random Number Generator

Postby herd on Fri Jul 03, 2009 12:05 pm

Ok sorry for double post, I did little research and found out that rand() is INDEED giving you NOT equally spaced random numbers.

If using in a game the chance calculation would not be fair in some cases as you can imagine :(
herd
 
Posts: 15
Joined: Wed Jul 01, 2009 12:42 pm

Re: Random Number Generator

Postby antiRTFM on Fri Jul 03, 2009 12:33 pm

great job doing research on your own!

pseudo random number generators are good enough for little programs and games. with some more research you can find better such solutions (hint: boost mersenne twister)

you don't need anything more random than this unless your making some serious secutiy cryptology software.
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Random Number Generator

Postby noobgrammer on Tue Sep 22, 2009 2:48 pm

Am I doing something wrong here I get the same first number every time

Code: Select all
#include <iostream>
#include <ctime>
#include <cmath>


int get_rand(int num1, int num2);

int main()
{
   srand((unsigned)time(NULL));
   int number;
   for (int i = 0; i < 10; i++)
   {
      number = get_rand(1,10);
      std::cout<< number << "\n";
   }
}

int get_rand(int num1, int num2)
{
   int &rangeMin = num1 > num2 ? num2 : num1;
   int &rangeMax = num1 > num2 ? num1 : num2;
   return (int)floor(rand() / (RAND_MAX + 1.0) * ((rangeMax - rangeMin)+ 1 ) + rangeMin);
}
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Random Number Generator

Postby antiRTFM on Tue Sep 22, 2009 8:55 pm

This behavior is to be expected when running and re-running your program repeatedly within a short period of time and has to do with how rand() works internally.

You might want to call your getrand function a couple of times at the beginning of your program if you want to hide this effect, but it really isnt necessary since ultimately your program won't be run repeatedly within the same couple of minutes.
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Random Number Generator

Postby Koeienboei on Tue Sep 29, 2009 10:56 am

maybe this is the wrong forum for this, but can i use this in C?
User avatar
Koeienboei
 
Posts: 11
Joined: Fri May 01, 2009 12:42 pm
Location: Holland

Re: Random Number Generator

Postby noobgrammer on Sun Oct 04, 2009 10:42 am

Well I don't know if they changed the rand() in VS because I get the same results as yellowarrowplane was using the more complicated rand version

I did this

Code: Select all
#include <iostream>
#include <ctime>

int main()
{
   srand((unsigned)time(NULL));
   
   int number = 0;
   int one = 0;
   int two = 0;
   int three = 0;
   int four = 0;
   int five = 0;
   int six = 0;
   int seven = 0;
   int eight = 0;
   int nine = 0;
   int ten = 0;

   for (int i = 0; i < 10000; i++)
   {
      number = rand() % 10 + 1;

      if (number == 1)
      {
         one += 1;
      }
      else if (number == 2)
      {
         two += 1;
      }
      else if (number == 3)
      {
         three += 1;
      }
      else if (number == 4)
      {
         four += 1;
      }
      else if (number == 5)
      {
         five += 1;
      }
      else if (number == 6)
      {
         six += 1;
      }
      else if (number == 7)
      {
         seven += 1;
      }
      else if (number == 8)
      {
         eight += 1;
      }
      else if (number == 9)
      {
         nine += 1;
      }
      else if (number == 10)
      {
         ten += 1;
      }
   }

   std::cout<< one << "\n" << two << "\n"  << three << "\n"<< four << "\n" << five << "\n"
      << six << "\n" << seven << "\n" << eight << "\n" << nine << "\n" << ten << "\n";
}


you can change the number in the for loop and it will come out with a matching uniform number each time and is random

I also did a test in C with the same results though I still had to save as a .cpp I don't know if I was doing something wrong it didn't like the .c

I don't know if its a VS deal or if any compiler will return a uniform number each time but still random
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Random Number Generator

Postby antiRTFM on Sun Oct 04, 2009 9:48 pm

you seem to be using your own formula "rand() % 10 + 1"

use the formula from the first post
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

PreviousNext

Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests