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 10-May-2004, 19:06
KrissKross KrissKross is offline
New Member
 
Join Date: Apr 2004
Posts: 14
KrissKross is on a distinguished road

Random numbers


In C how do u generate a random number between 1 and 6. I know it has something to do with the RND function from stdlib.h but I don't how to use it.
  #2  
Old 10-May-2004, 20:21
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
CPP / C++ / C Code:
//seed random
srand((unsigned) time(NULL));
//get random
int tmp=rand()%6+1;
  #3  
Old 10-May-2004, 20:34
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
You do have to include stdlib.h as well.

Also, keep in mind that you should only seed the random number function once in your program or you may get unrandom random numbers. There are several posts here that discuss this, but there is nothing really random about the random numbers that this function will give you. When you seed the number generator it selects a predefined list of numbers based on that seed. So if you seed it with the same number, your sequence will always be identical.

Cheers,
d
  #4  
Old 13-May-2004, 17:51
KrissKross KrissKross is offline
New Member
 
Join Date: Apr 2004
Posts: 14
KrissKross is on a distinguished road
Hmm didnt realy work, I need my program to generate 5 different random numbers and assingn them to elements 0 through 4 of an array. When I run the program it always generates the same number for all 5 elements. Is there some way I can generate 5 different random numbers? My program needs to be able to simulate 5 dice and I cant have them all being 1 or 4 or 6.
  #5  
Old 13-May-2004, 18:49
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
put srand(time(NULL)); at the very beginning of your program. and then whenever you need random numbers, just use the rand() function. you must use srand function only once in your program!
__________________
spasms!!!
  #6  
Old 13-May-2004, 20:31
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 KrissKross
Hmm didnt realy work, I need my program to generate 5 different random numbers and assingn them to elements 0 through 4 of an array. When I run the program it always generates the same number for all 5 elements. Is there some way I can generate 5 different random numbers? My program needs to be able to simulate 5 dice and I cant have them all being 1 or 4 or 6.

A random number is just a random number there is no guarantee that you will get just a single number in a selection set, in fact it would be pretty rare. What you need to do is to set up a loop that will check the previous numbers to see if they are similar. Something like:

CPP / C++ / C Code:
srand((unsigned) time(NULL));
for(x=0;x<5;x++){
  numbers[x] = rand()%6+1;
  same = 0;
  do{
    for(y=0;y<x;y++){
      if(numbers[x] == numbers[y]){
        same = 1;
        break;
      }
    }
  }while(!same);
}

Thats written on the fly (I didn't compile it), but it should give you the idea.

HTH,
d
 
 

Recent GIDBlogWriting a book by crystalattice

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
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Random() : Make each number onlu appear once NiXeN C++ Forum 3 13-Jan-2004 05:47
Need a script for selecting random tabels and from those tabels selecting random ques mlt MySQL / PHP Forum 2 12-Sep-2003 10:01
Best way to validate numbers (ids)? JdS MySQL / PHP Forum 1 20-Jan-2003 04:55
Random no longer JUST random... JdS GIDTopsites™ 0 12-Jan-2003 09:57

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

All times are GMT -6. The time now is 01:48.


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