css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <section>
	<h1>Random numbers generator</h1>
	<h4>Generate any number of random values with or without repeats</h4>
	<div class="group">
		<label for="minimum">Minimum:</label>
		<input type="number" step="1" id="minimum"/>
	</div>
	<div class="group">
		<label for="maximum">Maximum:</label>
		<input type="number" step="1" id="maximum"/>
	</div>
	<div class="group">
		<label for="resultcount">How many results?</label>
		<input type="number" min="1" step="1" id="resultcount" placeholder="1"/>
	</div>
	<div class="boxgroup">
		<input type="checkbox" id="norepeat"/>
		<label for="norepeat">No repeat results</label>
	</div>
	<div class="boxgroup">
		<input type="checkbox" id="sort"/>
		<label for="sort">Order results numerically</label>
	</div>
	<button onClick="crunch()">Generate</button>
		<div id="result"></div>
</section>
            
          
!
            
              $darkGray: #53565a;
$yellow: #ffd100;

@mixin flexy {
	display: flex;
	justify-content: flex-start;
	flex-wrap: wrap;
}

* {
	box-sizing: border-box;
}

body {
	background: #f6ffec;
	margin: 0;
	font-family: Sahitya;
	font-size: calc(.8rem + 1vw);
	color: $darkGray;
	section{
		@include flexy;
		position: relative;
		width: 100%;
		max-width: 24em;
		min-height: 100vh;
		margin: auto;
		padding: 1em;
		align-items: center;
		align-content: center;
		h1 {
			font-size: 1.8em;
			margin: 0;
			line-height: .9;
		}
		h4 {
			font-size: .7em;
			font-weight: normal;
			margin: .5em 0 32px;
		}
		.group, .boxgroup{
			@include flexy;
			justify-content: space-between;
			width: 100%;
			flex-wrap: wrap;
			margin-bottom: 1em;
			label {
				flex: 1 0 auto;
				line-height: 2;
				position: relative;
				top: 0em;
				border-bottom: 1px solid $darkGray;
			}
			input {
				flex: 10 0 auto;
				padding: 0 0 .1em .5em;
				font-family: Sahitya;
				font-size: 22px;
				border: none;
				border-bottom: 1px solid $darkGray;
				background: none;
				margin: 0;
				text-align: right;
				&:focus {
					outline: none;
				}
			}
		}
		.boxgroup {
			width: 100%;
			justify-content: flex-start;
			label{
				text-align: left;
				flex: 10 0 auto;
				padding-left: 1.2em;
				&:before {
					content: '';
					font-size: 120%;
					line-height: .5em;
					width: .6em;
					text-indent: .075em;
					height: .6em;
					border: 1px solid $darkGray;
					position: absolute;
					left: 0;
					bottom: .5em;
				}
				&.checked {
					font-weight: bold;
				}
				&.checked:before {
					content: 'x';
				}
			}
			input {
				width: auto;
				flex: 1 0 auto;
    		margin: 0.32em 0.4em 0 0;
				border-bottom: 1px solid black!important;
				display: none;
			}
		}
		button {
			font-family: Sahitya;
			padding: .5em 1em;
			font-size: 18px;
			margin-top: 32px;
			background: $darkGray;
			border: 1px solid $darkGray;
			color: #fff;
			cursor: pointer;
			transition: all .2s ease-in-out;
			&:hover {
				background: #a7a8aa;
				transform: scale(1.1);
			}
			&:focus {
				outline: none;
				background: $yellow;
				color: $darkGray;
			}
		}

		#result {
			width: 100%;
			height: 1em;
			line-height: 1em;
			font-size: 36px;
			text-align: left;
			margin-top: .5em;
			.error {
				color: red;
				font-size: 40%;
				line-height: .4em;
				animate: bounce .5s ease-in-out forward;
			}
		}
	}
}

@keyframes bounce {
	0% {
		transform: translateY(-.25em);
	}
	25% {
		transform: translateY(0);
	}
	50% {
		transform: translateY(-.25em);
	}
	75% {
		transform: translateY(0);
	}
}

@media (max-width: 600px) {
	body section .group label, body section .boxgroup label {
		width: 100%;
		border: none;
	}
	body section .group input {
		text-align: left;
	}
}
            
          
!
            
              	var resultArea = document.querySelector('#result');
	var noRepeatBox = document.querySelector('#norepeat');
	var sortBox = document.querySelector('#sort');

	var noRepeatLabel = document.querySelector('label[for=norepeat]')
	var sortLabel = document.querySelector('label[for=sort]')


	noRepeatBox.addEventListener('click', function(){
		noRepeatLabel.classList.toggle('checked');
	});

	sortBox.addEventListener('click', function(){
		sortLabel.classList.toggle('checked');
	});

function crunch(){
	var noRepeat = document.querySelector('#norepeat').checked;
	var sort = document.querySelector('#sort').checked;
 	var min = Number(document.querySelector('#minimum').value);
	//console.log(min);
	var max = Number(document.querySelector('#maximum').value);
	//console.log(max);
	var resultCount = Number(document.querySelector('#resultcount').value);
	var resultsArray = [];
	
	resultArea.innerHTML = '';
	
	if((max - min) == 0) {
		resultArea.innerHTML = '<span class="error">Please choose a valid range.</span>'
		return;
	} else if (max < min) {
		resultArea.innerHTML = '<span class="error">Invalid number range</span>'
		return;
	} else if (noRepeat && resultCount > ((max - min) + 1)) {
		resultArea.innerHTML = '<span class="error">Range is too small to generate only unique numbers</span>'
		return;
	} 
	if(!resultCount) {
		resultCount = 1;
	}
	
	function newNumber() {
		if (min == 0) {
			var result = Math.floor(Math.random() * (max + 1));
		} else if (min < 0 && max >= 0) {
			var result = Math.ceil(Math.random() * (min - 1)) + Math.floor(Math.random() * (max + 1));
		} else if (min < 0 && max < 0) {
			var result = (Math.ceil(Math.random() * (min - 1))) + max;
		} else {
			var result = Math.floor(Math.random() * max + 1);
		}
		if(result < min || noRepeat && resultsArray.includes(result)) {
			return newNumber();
		}
		//console.log(result);
		resultsArray.push(result);
	}

	for(i = 1; i<=resultCount; i++){
		newNumber();
	}
	console.log(resultsArray);
	if(sort){
		resultsArray == resultsArray.sort((a, b) => a - b);
	}
	resultArea.innerHTML += resultsArray.join(', ');
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console