C++ Learning Community Forum
September 11, 2010, 02:07:24 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Random numbers  (Read 707 times)
Greg
N00b!!1
*
Posts: 10


View Profile
« on: March 14, 2007, 12:07:35 AM »

This is just a quickie program I made, but it brings up a good point.
wtf at the pseudo random number generator?
How could I generate a more random random number?
Code:
#include <iostream>
#include <fstream>
#include "random.h"

using namespace std;

void random_song(int);
int menu();

int main()
{
  int choice = menu();
  if(choice==1)
  {
    int length;
cout << "\nHow long should the song be (# of notes, max 100)?\n";
cin >> length;
    //enum, gets a random number, assigned to a letter, makes a text file with those letters, and you're done.
random_song(length);
  }
  return 0;
}

int menu()
{
  int c;
  cout << "/nWelcome o the random song generator, what would you like to do?\n"
       << "0) Quit\n"
   << "1) Create a random song\n";
  cin >> c;
  if(c==1)
  {
    return c;
  }
  else
  {
    return 0;
  }
}

void random_song(int l)
{
  int note;
  ofstream myFile;
  myFile.open("random_song.rsong",ios::app);
  for(int i=0; i<l; i++)
  {
    note = random_num(6);
switch (note)
{
  case 0:
    //Write A
myFile << 'A';
    break;
  case 1:
    //Write B
myFile << 'B';
break;
  case 2:
    //write C
myFile << 'C';
break;
  case 3:
    //write D
myFile << 'D';
break;
  case 4:
    //write E
myFile << 'E';
break;
  case 5:
    //write F
myFile << 'F';
break;
  default:
    //write G
myFile << 'G';
break;
}
  }
  myFile.close();
}

 
Code:
#ifndef RANDOM_H_
#define RANDOM_H_

inline int random_num(int max_rand)
{
     int rand_num;
     max_rand++;
srand( (unsigned)time( NULL ) );
rand_num=rand()%max_rand;
return rand_num;
}
#endif

Logged
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1241



View Profile
« Reply #1 on: March 14, 2007, 12:50:19 AM »

the reason it isn't working is because you are calling srand(time(0)); with the same value before every time you call rand(). since computers are so fast, this calls the same seed, and starts the randoms at the beginning of the pattern each time. call srand(time(0));  right before the for loop with the switch in it, and remove it from the random_num function and youll be fine.  Smiley
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
zaqufant
Farmer Brown
Dr. of C++ology
****
Posts: 963


Harder, better, faster, stronger.


View Profile
« Reply #2 on: March 14, 2007, 01:01:24 AM »

But don't be fooled. the numbers are still pseudo-random.
Logged
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 636



View Profile
« Reply #3 on: March 14, 2007, 01:19:01 AM »

I refer you to an article by one of our member : http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx
Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1241



View Profile
« Reply #4 on: March 14, 2007, 01:19:26 AM »

yeah thats right. Computers are deterministic by nature, so thats about all you can do without sampling some sort of external data in. I saw an article once about random numbers being generated through the use of a lava lamp and a webcam. interesting stuff. One good thing about psudo rand numbers, is they can sure help debugging, and they are good enough for most purposes  Cool

Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
FrozenKnight
Extends without bound
Global Moderator
Dr. of C++ology
*****
Posts: 558


Do it yourself it's the only way to learn.


View Profile
« Reply #5 on: March 21, 2007, 03:53:07 PM »

if you want you can link to my rand.dll i created a while back in ASM it uses the | algorithm to generate random numbers in a very large period. i have tried to predict the results by using a debugger but was unable. it uses a 19968 bit seed (which you can generate off of a 32 bit number using it's init function, or you can call it straight out as it will auto generate it's seed and begin generating numbers without you ever calling init)
Logged


Imagine the impossible, then make it happen.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!