GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Mar-2004, 18:16
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough

Randomness but in a good way


I was wondering if there was anyway of using srand to create a number within given boundries, 6-800(or something similar. I had a thought of a way to do it but wasnt sure if once again there was a better way to do what seems like a simpe task. But my thoughts go along the line of:
CPP / C++ / C Code:
//x being an integer and the random number
if(x>29999)
{
  x=x-30000;
}
else
{
  if(x>19999)
  {
    x=x-20000;
  }
  else
  {
    if(x>9999)
      {
      x=x-10000
//...//
   if(x>800)
   {
   x=x-199
   }
else
{
x=x
}

This would only work if my knowledge of an integer being between -32768 and 32768 is correct otherwise it gets unbelievably huge, at which point i'd leave it out of my program. I can ignore the negative numbers as I could square then square root the number.
  #2  
Old 26-Mar-2004, 19:06
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Well, I know this equation will give you a random number between 1 and 4...

num = (rand() % 4) + 1;

So...

num = (rand() % 800) + 6;

...should give you a number between 6 and 800.
__________________
-Aaron
  #3  
Old 26-Mar-2004, 20:14
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I think it would be more like:

CPP / C++ / C Code:
num = (rand() % 795) + 6;

The modulus portion should produce a number between 0 and 794 (the remainder of integer division by 795 of any number) and then by adding 6 you should get 6 through 800.
  #4  
Old 26-Mar-2004, 22:34
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by dsmith
I think it would be more like:

CPP / C++ / C Code:
num = (rand() % 795) + 6;

The modulus portion should produce a number between 0 and 794 (the remainder of integer division by 795 of any number) and then by adding 6 you should get 6 through 800.
Right, my mistake. Mine will give you 1 through 805.
__________________
-Aaron
  #5  
Old 27-Mar-2004, 02:27
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
That's a clever little formula, I must remember it.
  #6  
Old 27-Mar-2004, 20:18
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by JdS
That's a clever little formula, I must remember it.
The generic form is:
Code:
num = ((rand() % ((MAX - MIN) + 1)) + MIN;
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #7  
Old 29-Mar-2004, 05:07
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
Is there anyway of getting the numbers to be "more random" its just whenever I run the program I get the same random numbers ihe same order eg. the first time I run the program I always get x1=554. Is there a way this would be a "real" random number?
CPP / C++ / C Code:
 Random()
{
x1 = num = (rand() % 795) + 6;
x2 = num = (rand() % 795) + 6;
x3 = num = (rand() % 795) + 6;
x4 = num = (rand() % 795) + 6; 
y1 = num = (rand() % 595) + 6;  
y2 = num = (rand() % 595) + 6; 
y3 = num = (rand() % 595) + 6; 
y4 = num = (rand() % 595) + 6;
w1 = num = (rand() % 5) + 1;
w2 = num = (rand() % 15) + 1;
w3 = num = (rand() % 17) + 1;
w4 = num = (rand() % 7) + 1;
n = (rand() % 91) + 8; 
return 1;
}
  #8  
Old 29-Mar-2004, 06:58
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
Ignore where it says "num" in the code, i simpy forgot to remove it after I first put it into the program.
  #9  
Old 29-Mar-2004, 08:54
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by warny_maelstrom
Ignore where it says "num" in the code, i simpy forgot to remove it after I first put it into the program.

Hi Warny. In order to make your "random" numbers go in random sequences, you need to used the srand(int) command at the start of your program with a different number. This is usually accomplished by passing the current time to the srand command. Search this forum as this has been well covered previously and there are several good examples.

Cheers,
d
  #10  
Old 29-Mar-2004, 11:00
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
ok i checked previous posts and also went on to tutorial sites and even tried the example given with my compiler(big mistake). Im still kind of lost on the subject and thought I had it but dont understand entirely how each line interacts. This is just a little program I made to print a random number but as b4 the number returned is always the same, I assume its that I hav'nt linked the time to the rand statement but this is what I don't understand:
CPP / C++ / C Code:
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>  
int main(void)
{ 
  int i; 
  srand(time(NULL));
  i=((rand()%795)+5);
  printf("random num:%d\n",i);
  return 0;
} 
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hey, i need a good upload feature for my site alex Web Design Forum 2 11-Feb-2004 11:59
I Hope I'll have a good stay here :) cityit New Member Introductions 2 28-Dec-2003 23:49
I need a good wed designer Kulok New Member Introductions 4 14-Sep-2003 20:35
A very good sitemap script jrobbio MySQL / PHP Forum 3 03-Mar-2003 02:07
A good pop-under script? JdS Web Design Forum 4 18-Jun-2002 09:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.