GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Feb-2004, 11:50
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road

randomness


I'm learning C++ and want to write a program to shuffle a deck of cards. I've done it in 3 other languages, but I dont know any way to do random numbers (yet). so does anyone know a simple way to get a random number, or check the clock so I can use that to create a number or ANYTHING not waay complicated? It would be really nice. If not, i just get to follow the stupid book i have to learn other stuff. (which doesnt have any random/clock commands) thanks!
  #2  
Old 02-Feb-2004, 11:59
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
Hey ambeco, welcome to the forums!

In another thread, I was helping a guy get random numbers where he was using some random number generators. You can view this thread here.

The basic routine that he (jack) had written was:
CPP / C++ / C Code:
long ra(){
  long number;
  time_t t1;

  (void) time(&t1);

  srand48((long) t1);
  number = lrand48()%1000000000;

  return(number);
}

If you read through that thread though, the biggest problem is that the number is surprisingly unrandom. One thing that I suggested is to put in a sleep command to change the time value if you are going to be taking successive random numbers.

Let me know , if this helps or not.
Last edited by dsmith : 03-Feb-2004 at 22:05.
  #3  
Old 03-Feb-2004, 10:13
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road
Lightbulb

what about time


I think I can use the seconds on the clock to come up with sufficient randomness. does C++ have a command to take the time like VBASIC does? I dont understand enough C++ yet to understand how that bit that you gave me works. I'll see if I can figure out how to work it though. Thanks for what you already gave me!
BTW, heres what I had in mind:

int(sin(seconds)*.5+.5*(max-min))+min
  #4  
Old 03-Feb-2004, 10:29
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road
Unhappy

srand


I was experimenting with the bit you gave me, but it kept coming up with errors with the srand and lrand. I searched the link you gave me, but I couldnt find anything about these. I even tried including all of those files and still no good. What are these, and how do I use them? Thanks for anything you can help with!
  #5  
Old 03-Feb-2004, 13:20
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
Hi ambeco.

Sorry for the mishap with the program. Reading through what you want to do and my limited experience with random number generation, I think you may just want to use the random and srandom functions. You will probably only want to actually seed it one time for each time the program is ran.

Try this out & see if it gives you any results. This should basically just print 1000 random numbers to your screen (and they should all be different)
CPP / C++ / C Code:
#include <stdlib.h>				//This has the random & srandom functions
#include <time.h>


int main()
{
	time_t	seconds;
	int		x;

	time(&seconds);				//This gets the current time in seconds

	srandom((int) seconds);		//This seeds the random number generator.

	for(x=0;x<1000;x++)			//This will print 1000 random numbers.
		printf("Random number: %ld\n",random());
}

This compiled without a hitch using gcc under linux. This is pretty vanilla c code as far as I know you should be able to compile it anywhere... Let me know if you still have problems.
  #6  
Old 03-Feb-2004, 21:20
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
if u cannot understand the bottom part u can email:taychinghwa@hotmail.com or yahoo messager me:taychinghwa

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
  int b;
  srand((0));   // srand only can declare onces
                 // instead of srand((o)) u also can srand((NULL))
  for (int i=0; i<20; i++)
  {
  cout<<b<<"  ";
  b=rand()%5;      // random 5 digit only
  }              // rand() u want declare how many times also can
  cout<<endl<<endl;
  
  system("pause");
  return 0;
}
Last edited by dsmith : 03-Feb-2004 at 22:02. Reason: Added syntax highlighting
  #7  
Old 04-Feb-2004, 07:40
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
Hi Tay. Welcome to the forums!

First of all, thank you for the response! Sometimes it seems that I am the only one responding on this board. It's especially helpful, because I forget that people want C++ syntax

There have been several questions about random numbers and it has caused me to research it a bit. What I have figured out is that random numbers are pretty easy to replicate. In order to verify this, I downloaded and ran your routine. First of all, I did a small modification, ala:
CPP / C++ / C Code:
b=rand()%10000;
cout<<b<<"  ";

Your original code, printed b before anything was in it. (Now that was an original number). Also in order to get a five digit number, you need to % by 10000, not 5. Your original code gave random numbers between 0 and 5.

Then I ran it and here is the output
Code:
9383 886 2777 6915 7793 8335 5386 492 6649 1421 2362 27 8690 59 7763 3926 540 3426 9172 5736

I ran it again and again and again. Every time it output the exact same sequence above. Therefore, that is the reason to seed the random number with the time value as it will produce a different sequence every time you run it. I think that the man page for rand explains it best:

The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.


Finally, the difference between the rand() and random() function. On most compilers these are identical. However on some older implementations, rand is different. Once more to quote from the man page for rand:

However, on older rand() implementations, the lower-order bits are much less random than the higher-order bits.

So, in other words, using rand() is probably okay for 95% of the compilers out there.

Once again, I really appreciate your input and feedback. It is discussions like these that make these forums so useful!
Last edited by dsmith : 04-Feb-2004 at 08:10.
  #8  
Old 04-Feb-2004, 10:40
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road
Post

Thanks for the codes! dsmith, your code wouldnt compile, came up with several errors(my compiler wont do srandom, just srand, and didnt understand your printf), so what I did was took the part that grabs the time and stuck it into tay's program so that I get a sequence of random numbers, that changes every second. I can now modify this for my needs. Thanks you two! I think I will insert the bare random coding for other newbs to use for whatever simply:
CPP / C++ / C Code:
//include these
#include <iostream>
#include <cstdlib>
#include <time.h>
//in main:
  int b;
  time_t  seconds;        //not sure???
  time(&seconds);        //This gets the current time in seconds
  srand((int) seconds);   // srand seeds rand as seconds
//to get a random number
  b=rand()%5; //picks 0 to 4
  #9  
Old 04-Feb-2004, 13:35
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
sorry i forgot a little bit things
thx for infrom

instead of " srand((0)) "
change it to srand(time(0)) add a "time" inside the bracket
then all the digit will total different

#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
int b;
srand(time(0));

for (int i=0; i<20; i++)
{
b=rand()%10000;
cout<<b<<" ";
}
cout<<endl<<endl;

system("pause");
return 0;
}
  #10  
Old 04-Feb-2004, 13:42
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
dsmith is using the c way to code not c++ so u might be confuse it
actually almost is same 1, if u understand his code u should be know how to convert it in c++. if u cannot convert it mean u still not really fully understand it. try to learn it
programmer is learn algorithms not syntex, if we know algorithms we can apply in any type of language like c / c++ or java
same concept but different syntex :-)



Quote:
Originally Posted by ambeco
Thanks for the codes! dsmith, your code wouldnt compile, came up with several errors(my compiler wont do srandom, just srand, and didnt understand your printf), so what I did was took the part that grabs the time and stuck it into tay's program so that I get a sequence of random numbers, that changes every second. I can now modify this for my needs. Thanks you two! I think I will insert the bare random coding for other newbs to use for whatever simply:
CPP / C++ / C Code:
//include these
#include <iostream>
#include <cstdlib>
#include <time.h>
//in main:
  int b;
  time_t  seconds;        //not sure???
  time(&seconds);        //This gets the current time in seconds
  srand((int) seconds);   // srand seeds rand as seconds
//to get a random number
  b=rand()%5; //picks 0 to 4
 
 

Recent GIDBlogProblems with the Navy (Enlisted) 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

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

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


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