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

Think you can spy out that little nasty bug? Try debugging some code in here

random number

Postby learning in progress on Sat Aug 01, 2009 2:38 pm

This program runs fine but doesnt do what it is supposed to :roll:
Code: Select all
#include "stdafx.h"
#include <iostream>


using std::cout;
using std::cin;

int randomnumber();

int main()
{
   cout <<"Practice random number generator \n";
   randomnumber();
   for (int random=0;random<100;random++)
   {   cout <<randomnumber()<<"\t";
   if ((random+1) %5==0) cout<< "\n";}

   char f;
   cin>>f;
   return 0;
}

int randomnumber()
{
   static unsigned int random=5263;
   random=(21574*random+5484-69);
   return random %56846;
}

whats going on here :|
learning in progress
 
Posts: 11
Joined: Thu Jul 09, 2009 4:15 pm

Re: random number

Postby Knight313 on Mon Aug 03, 2009 11:05 pm

So I was looking at the output of that code and it seems your result is like a sin wave that eventually flattens out at a value of 53181, that number is simply a number that when input into your function results in the same number.

In a program that I wrote I had a problem where my program would ask the same number over and over agian. I needed to write extra logic to help catch those problems and take care of them. I think you are going to simply need to come up with a better function to randomize your number. I have never done "random numbers" before but I googled it once and found a bunch of articles on it. I would suggest reading how someone else did it.

Thats just a quick look at it, im no expert either so maybe someone else can give you a better answer.
Knight313
 
Posts: 64
Joined: Mon Aug 03, 2009 7:20 pm

Re: random number

Postby learning in progress on Tue Aug 04, 2009 10:39 am

Knight313 wrote:So I was looking at the output of that code and it seems your result is like a sin wave that eventually flattens out at a value of 53181, that number is simply a number that when input into your function results in the same number.

In a program that I wrote I had a problem where my program would ask the same number over and over agian. I needed to write extra logic to help catch those problems and take care of them. I think you are going to simply need to come up with a better function to randomize your number. I have never done "random numbers" before but I googled it once and found a bunch of articles on it. I would suggest reading how someone else did it.

Thats just a quick look at it, im no expert either so maybe someone else can give you a better answer.


Thanks for the suggestion
i tries srand(time(0)) and thn rand()
and it worked
Code: Select all
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
   srand(time(0));
   for (int count=0;count<10;count ++)
   {cout <<rand()%100<<"\t";
   
   if ((count+1) %5==0) cout<< "\n";}
   
   
   
   char f;
   cin>>f;
   return 0;
}


now i'm trying to figure out how to assign the random number to a variable :D
learning in progress
 
Posts: 11
Joined: Thu Jul 09, 2009 4:15 pm


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests