C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 10-02-2001, 11:39 PM   #1
Registered User
 
Join Date: Oct 2001
Posts: 75
Randomize??????

Hello


I was wondering


how do I randomize a value, I need this to decide random outcomes (obviously) thanks for any help simple code will do me

cheers

stealth
Stealth is offline  
Old 10-03-2001, 12:46 AM   #2
Registered User
 
Join Date: Oct 2001
Posts: 8
You can use the rand() function in cstdlib to get a random number.

It is used like this:

Code:
int x = rand() % 5; //x would be between 0 and 4
Before calling rand(), you should call srand() to seed the random number generator. You must always use a different number when calling srand(), the current time for instance, or else rand() will always give you the same numbers.

So you might want to do something like this:

Code:
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int RandInt(int a,int b)
{
   return a + rand() % (b - a + 1);
}

void main()
{
   //Seed rand() with current time
   srand(unsigned(time(NULL)));
   //x will be between 2 and 10, inclusive
   int x = RandInt(2,10);
   cout<<x;
}
LlamaDolittle is offline  
Old 10-03-2001, 02:32 AM   #3
Registered User
 
kitten's Avatar
 
Join Date: Aug 2001
Posts: 109
There is more information of randomizing in the FAQ.
__________________
Making error is human, but for messing things thoroughly it takes a computer
kitten is offline  
Old 10-03-2001, 04:47 AM   #4
Registered User
 
Join Date: Oct 2001
Posts: 75
ok

ok thanks for the help guys

cheers

stealth
Stealth is offline  
Old 10-09-2001, 08:33 AM   #5
Unregistered
Guest
 
Posts: n/a
I've tried doing random numbers using that same method and wanted to do random numbers between 1 and 100.

here are my results
2, 9,12,19,24, ..... etc, etc you get the idea I guess. There always increasing. Not really random. Is there anyway to get around this?
 
Old 10-09-2001, 07:41 PM   #6
Registered User
 
Join Date: Oct 2001
Posts: 3
Thumbs up

Yea there is you could do this


random{int n ) {


static bool seeded = false; /* used to ensure seeding done just one */

int seed;


if(!seeded) {

cout <<"Enter a value to randomize a value" << endl;
cin>> seed;
cin.ignore(80,'\n');
srand(seed);
seeded = true;
}

* then you call your rand() */
HelloWorld is offline  
Old 11-05-2001, 01:35 PM   #7
Registered User
 
Join Date: Nov 2001
Posts: 3
cout <<"Enter a value to randomize a value" << endl;
cin>> seed;
cin.ignore(80,'\n');
srand(seed);
seeded = true;

don't work in a visual C++ Dialog window or what ever you want ever there called. And I don't really want to have to enter a value every time I run it. Is there anyother way to do what I want?
Rick is offline  
Old 11-05-2001, 08:36 PM   #8
Registered User
 
Join Date: Oct 2001
Posts: 8
If you want different seeds everytime you could use the current time. So you would do something like this:

#include <time.h>

srand(unsigned(time(NULL)));

Then you will get different numbers from rand() every time without having to specify a seed value.
LlamaDolittle is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wiki FAQ dwks General Discussions 192 04-29-2008 01:17 PM
Help with FAQ JoshG Game Programming 19 10-29-2002 07:31 PM
FAQ Check/Lock RoD A Brief History of Cprogramming.com 2 10-15-2002 11:21 AM


All times are GMT -6. The time now is 08:43 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22